Filter out an array with null values, underscore

前端 未结 5 1435
情歌与酒
情歌与酒 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:45

    From underscore documentation

    without_.without(array, *values) 
    Returns a copy of the array with all instances of the values removed.
    

    So just use this method

    var a = [null, {name:'John'}, null, {name:'Jane'}]
    a = _.without(a, null);
    

提交回复
热议问题