[英]Filter [empty] array object
I'm deleting some array object in traditional way: delete subDirectories[index]
. 我正在以传统方式删除一些数组对象: delete subDirectories[index]
。 So, just after that this object is changing to [empty]
one. 因此,在此之后,此对象变为[empty]
。 Now, how to filter that, undefined, bool, NaN
nothing works. 现在,如何过滤undefined, bool, NaN
。 I'm working with Vue.js and this contains an vuex
action. 我正在使用Vue.js,它包含一个vuex
操作。 Can anybody help? 有人可以帮忙吗?
If you want to delete all null, undefined, (or any false-like) values in an array, you can just do: 如果要删除数组中的所有null,undefined(或任何类似false的值),则可以执行以下操作:
var arr = [1,3,5, null, False];
var res = arr.filter(val=>val);
console.log(res); // [1,3,5]
Alternatively, you can explicitly remove null and undefined: 或者,您可以显式删除null和undefined:
var res = arr.filter(val => (val!==undefined) && (val!==null));
console.log(res); // [1,3,5]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.