gpt4 book ai didi

javascript - 如何在js中删除数组的最后一个元素

转载 作者:行者123 更新时间:2023-12-04 16:11:19 25 4
gpt4 key购买 nike

我有一个二维数组,其中最后一个元素是空的。像这样:

data = [["1","2","3","","",""],["4","5","6","x","f",""],["7","8","9","",""]]

现在我想删除空元素并使用 .length 获得正确的新尺寸

我试过这样但它不起作用:

        for (i = 0; i != data[2].length-1; i++) {
if (data[2][i].isEmpty()) {
data[2] = data[2].splice(i, 1);
}
}

有什么好的解决办法吗?

最佳答案

您可以在过滤后使用 Boolean 作为回调来映射数组以保留 truthy元素。

var data = [["1", "2", "3", "", "", ""], ["4", "5", "6", "x", "f", ""], ["7", "8", "9", "", ""]];

data = data.map(a => a.filter(Boolean));

console.log(data);

关于javascript - 如何在js中删除数组的最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48391335/

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