Filter out an array with null values, underscore

前端 未结 5 1442
醉话见心
醉话见心 2021-02-11 18:32

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:36

    The best solution is to use compact, but the default behaviour of the filter function when you don't include a specific truth test function is to remove falsy values

    For example:

    _.filter([null, {name:'John'}, null, {name:'Jane'}])
    

    returns a object array without the nulls:

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

提交回复
热议问题