9

I'm working with this structure:

[
   [
      {
         "comments":"asd",
         "movement":"Back Squat",
         "userID":"wDHZv3OL55SIymHkhMUejNleNkx1",
         "weight":"330"
      }
   ],
   [
      {
         "comments":"asd",
         "movement":"Bench Press",
         "userID":"wDHZv3OL55SIymHkhMUejNleNkx1",
         "weight":"100"
      }
   ],
   [
      {
         "comments":"Comment",
         "movement":"Clean",
         "userID":"wDHZv3OL55SIymHkhMUejNleNkx1",
         "weight":"195"
      }
   ],
   [

   ],
   [

   ],
   [
      {
         "comments":"Front squat comment alpha",
         "movement":"Front Squat",
         "userID":"wDHZv3OL55SIymHkhMUejNleNkx1",
         "weight":"315"
      }
   ],
   [

   ],
   [

   ],
   [

   ],
   [

   ],
   [

   ],
   [
      {
         "comments":"abc",
         "movement":"Strict Press",
         "userID":"wDHZv3OL55SIymHkhMUejNleNkx1",
         "weight":"155"
      }
   ]
]

This is the input I'm using in JSON format. As you can see there are multiple empty arrays.

How would I go about filtering through these arrays and remove the empty ones?

3
  • "This structure": that is not valid JavaScript notation for a single structure. Could you provide the input and desired output in JavaScript notation? Commented Jun 17, 2017 at 9:12
  • I've added the input Commented Jun 17, 2017 at 9:16
  • 1
    The provided answers will do the job. Your claim that they return an empty array is strange. If that really is JSON (i.e. text format), then of course you first need to parse it into a real array. Commented Jun 17, 2017 at 9:20

3 Answers 3

17

Use the native Array#filter or lodash's _.filter(), and keep the sub arrays with length other than 0.

Array#filter

var arrs = [[{"comments":"asd","movement":"Back Squat","userID":"wDHZv3OL55SIymHkhMUejNleNkx1","weight":"330"}],[{"comments":"asd","movement":"Bench Press","userID":"wDHZv3OL55SIymHkhMUejNleNkx1","weight":"100"}],[{"comments":"Comment","movement":"Clean","userID":"wDHZv3OL55SIymHkhMUejNleNkx1","weight":"195"}],[],[],[{"comments":"Front squat comment alpha","movement":"Front Squat","userID":"wDHZv3OL55SIymHkhMUejNleNkx1","weight":"315"}],[],[],[],[],[],[{"comments":"abc","movement":"Strict Press","userID":"wDHZv3OL55SIymHkhMUejNleNkx1","weight":"155"}]];

var result = arrs.filter(function(sub) {
  return sub.length;
});

console.log(result);

Lodash's _.filter() with _.size:

var arrs = [[{"comments":"asd","movement":"Back Squat","userID":"wDHZv3OL55SIymHkhMUejNleNkx1","weight":"330"}],[{"comments":"asd","movement":"Bench Press","userID":"wDHZv3OL55SIymHkhMUejNleNkx1","weight":"100"}],[{"comments":"Comment","movement":"Clean","userID":"wDHZv3OL55SIymHkhMUejNleNkx1","weight":"195"}],[],[],[{"comments":"Front squat comment alpha","movement":"Front Squat","userID":"wDHZv3OL55SIymHkhMUejNleNkx1","weight":"315"}],[],[],[],[],[],[{"comments":"abc","movement":"Strict Press","userID":"wDHZv3OL55SIymHkhMUejNleNkx1","weight":"155"}]];

var result = _.filter(arrs, _.size);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

9

If all the items in the top level array are arrays then you could lodash's reject with the isEmpty predicate.

let result = _.reject(data, _.isEmpty);

isEmpty will also return true for empty objects amongst other thing so if your top level array can contain such items then to just remove empty arrays you could compose a new function to return just empty arrays and use that as the predicate to reject:

let isEmptyArray = item => _.isArray(item) && _.isEmpty(item);

let result = _.reject(data, isEmptyArray);

Comments

1

Test each array in turn to see if it has a non-zero (truthy) length. If it does, put it in your new array.

var array_of_arrays = [[1], [1,1], [], [], [1]];
var array_of_non_empty_arrays = array_of_arrays.filter((array) => array.length);
console.log(array_of_non_empty_arrays);

3 Comments

This is returning a single empty array as the result.
You can use destructuring to shorten it a bit filter(({ length }) => length)
@JoeBerthelot — It doesn't in the live demo embedded in the answer. I had to construct the input myself through since you failed to provide a minimal reproducible example in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.