gpt4 book ai didi

javascript - 从父数组中过滤一个空数组

转载 作者:行者123 更新时间:2023-11-30 11:10:36 24 4
gpt4 key购买 nike

如何在添加新元素之前从父数组中完全删除一个空数组。

我有一个看起来像这样的结构

var parentArray = [ ['123', '345'], [], ['12'] ]

我想在添加新数组之前删除索引为 1 的数组。

我已经试过了,但它没有删除它:

parentArray.filter((array, index) => {
if (array.length === 0) {
parentArray.splice(index, 1);
}
})
parentArray.push(['435']);

console.log(parentArray); //Still give [["123", "345"], [], ["12"]]

最佳答案

你不应该在过滤方法中改变你的数组。如果要保留该项目,回调函数应返回 true,否则返回 false。

const parentArray = [['123', '345'], [], ['12']];

const filteredArray = parentArray.filter( item => item.length !== 0);

console.log(filteredArray)

关于javascript - 从父数组中过滤一个空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53881248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com