如何在JavaScript中删除数组中的空元素?
是否有一种简单的方法,或者我需要循环并手动删除它们?
如何在JavaScript中删除数组中的空元素?
是否有一种简单的方法,或者我需要循环并手动删除它们?
当前回答
这可能会帮助您:https://lodash.com/docs/4.17.4#remove
var details = [
{
reference: 'ref-1',
description: 'desc-1',
price: 1
}, {
reference: '',
description: '',
price: ''
}, {
reference: 'ref-2',
description: 'desc-2',
price: 200
}, {
reference: 'ref-3',
description: 'desc-3',
price: 3
}, {
reference: '',
description: '',
price: ''
}
];
scope.removeEmptyDetails(details);
expect(details.length).toEqual(3);
scope.removeEmptyDetails = function(details){
_.remove(details, function(detail){
return (_.isEmpty(detail.reference) && _.isEmpty(detail.description) && _.isEmpty(detail.price));
});
};
其他回答
foo = [0, 1, 2, "", , false, 3, "four", null]
foo.filter(e => e === 0 ? true : e)
回报
[0, 1, 2, 3, "four"]
如果你确定你的数组中没有0,那么它看起来会更好一些:
foo.filter(e => e)
要移除孔,应使用
arr.filter(() => true)
arr.flat(0) // New in ES2019
对于删除孔、空和未定义:
arr.filter(x => x != null)
用于删除hole和falsy(null,undefined,0,-0,0n,NaN,“”,false,document.all)值:
arr.filter(x => x)
arr=[,null,(void 0),0,-0,0n,NaN,false,“”,42];console.log(arr.filter(()=>true));//[null,(void 0),0,-0,0n,NaN,false,“”,42]console.log(arr.filter(x=>x!=null));//[0,-0,0n,NaN,假,“”,42]console.log(arr.filter(x=>x));//[42]
注:
孔是一些没有元素的数组索引。
arr = [, ,];
console.log(arr[0], 0 in arr, arr.length); // undefined, false, 2; arr[0] is a hole
arr[42] = 42;
console.log(arr[10], 10 in arr, arr.length); // undefined, false, 43; arr[10] is a hole
arr1 = [1, 2, 3];
arr1[0] = (void 0);
console.log(arr1[0], 0 in arr1); // undefined, true; a[0] is undefined, not a hole
arr2 = [1, 2, 3];
delete arr2[0]; // NEVER do this please
console.log(arr2[0], 0 in arr2, arr2.length); // undefined, false; a[0] is a hole
上述所有方法都返回给定数组的副本,而不是就地修改它。
arr = [1, 3, null, 4];
filtered = arr.filter(x => x != null);
console.log(filtered); // [1, 3, 4]
console.log(arr); // [1, 3, null, 4]; not modified
干净的方法。
var arr = [0,1,2,"Thomas","false",false,true,null,3,4,undefined,5,"end"];
arr = arr.filter(Boolean);
// [1, 2, "Thomas", "false", true, 3, 4, 5, "end"]
以上答案都不适用于所有类型。下面的解决方案将删除null、undefined、{}[]、NaN,并保留日期字符串,最好的是它甚至从嵌套对象中删除。
function removeNil(obj) {
// recursively remove null and undefined from nested object too.
return JSON.parse(JSON.stringify(obj), (k,v) => {
if(v === null || v === '') return undefined;
// convert date string to date.
if (typeof v === "string" && /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ$/.test(v))
return new Date(v);
// remove empty array and object.
if(typeof v === 'object' && !Object.keys(v).length) return undefined;
return v;
});
}
函数removeNil(obj){//递归地从嵌套对象中删除null和undefined。返回JSON.parse(JSON.stringify(obj),(k,v)=>{如果(v===null||v==='')返回undefined;//将日期字符串转换为日期。if(typeof v==“string”&&/^\d\d\d\d-\d\dT\dd:\d\d:\d\d.d.d\dZ$/.test(v))返回新日期(v);//删除空数组和对象。if(typeof v=='object'&&!object.keys(v).length)返回undefined;返回v;});}常量ob={s: “a”,b: 43中,国家:['a','b','c'],l: 空,n: {ks:“a”,efe:null,ce:“”},d: new Date(),nan:nan,k: 未定义,emptyO:{},emptyArr:[],}常量输出=removeNil(ob);console.log(输出);console.log('测试:',ob.countries.length,typeof(ob.d))
只需使用array.filter(字符串);它返回javascript中数组的所有非空元素