Javascript/lodash how to remove empty arrays

16,018

Solution 1

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>

Solution 2

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);

Solution 3

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);
Share:
16,018
Joe Berthelot
Author by

Joe Berthelot

Updated on June 25, 2022

Comments

  • Joe Berthelot
    Joe Berthelot almost 2 years

    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?

  • Joe Berthelot
    Joe Berthelot almost 7 years
    This is returning a single empty array as the result.
  • Ori Drori
    Ori Drori almost 7 years
    You can use destructuring to shorten it a bit filter(({ length }) => length)
  • Quentin
    Quentin almost 7 years
    @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.