我有这个数组:
[null, {name:'John'}, null, {name:'Jane'}]
我想删除空值。使用 underscore 有简单的方法可以做到吗?
我有这个数组:
[null, {name:'John'}, null, {name:'Jane'}]
我想删除空值。使用 underscore 有简单的方法可以做到吗?
尝试使用_.without(array, *values)
,它会删除您不需要的所有值。在您的情况下,*values == null。
This will work for you
Filter
_.filter(arr,function (value) {
return value!==null;
})
拒绝
_.reject(arr,function (value) {
return value===null;
})
来自下划线文档
without_.without(array, *values)
Returns a copy of the array with all instances of the values removed.
所以只需使用这种方法
var a = [null, {name:'John'}, null, {name:'Jane'}]
a = _.without(a, null);
var everythingButTheNulls = _.reject(array, _.isNil);
- Nawlbergs