Javascript/lodash how to remove empty arrays

孤者浪人 提交于 2021-02-07 03:36:17

问题


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?


回答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>



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



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


来源:https://stackoverflow.com/questions/44602661/javascript-lodash-how-to-remove-empty-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!