如何从 JavaScript 中的数组中删除空元素?

const array = [0,1,"",2,,3,"",3,a,4,,4,,5,6,NaN,false,0,-1];
const filtered = array.filter((item) => {
    
    
    return item !== null && typeof item !== "undefined" && item !== "";
});
console.log(filtered); // [ 0, 1, 2, 3, 3,'a', 4, 4, 5, 6, NaN, false,0,-1 ]

猜你喜欢

转载自blog.csdn.net/weixin_55042716/article/details/134693026