Filter out an array with null values, underscore

前端 未结 5 1418
情歌与酒
情歌与酒 2021-02-11 17:54

I have this array:

[null, {name:\'John\'}, null, {name:\'Jane\'}]

I want to remove the null values. Is there an easy way to do this with unders

5条回答
  •  醉梦人生
    2021-02-11 18:37

    If the array contains either nulls or objects then you could use compact:

    var everythingButTheNulls = _.compact(list);
    

    NB compact removes all falsy values so if the array could contain zeros, false etc then they would also be removed.

    Could also use reject with the isNull predicate:

    var everythingButTheNulls = _.reject(array, _.isNull);
    

提交回复
热议问题