gpt4 book ai didi

javascript - 如何从数组中删除数组?

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

这是我的数组:

var myarray = [["d1", "sections/Dashboard-summary.html", "Dashboard"], 
["add", null, ""],
["20", "sections/MW-1-summary.html", "MW-1"],
["21", "sections/SB-5-summary.html", "SB-5"]]

如何删除第二个元素 ["add", null, ""] 以便新数组为

[["d1", "sections/Dashboard-summary.html?781", "Dashboard"],
["20", "sections/MW-1-summary.html?903", "MW-1"],
["21", "sections/SB-5-summary.html?539", "SB-5"]]

该元素可能并不总是位于第二个位置,但它的第一个值将始终为“add”。如何删除第一个值为“add”的 (myarray[1][0]) 的数组?

最佳答案

That element might not always be in the second position but its first value will always be "add". How do I remove the array with the first value (myarray[1][0]) of "add"?

使用 splice() 的循环.

for (var i = 0, myarrayLength = myarray.length; i < myarrayLength; i++) {
if (myarray[i][0] === 'add') {
myarray.splice(i, 1);
break;
}
}

jsFiddle .

关于javascript - 如何从数组中删除数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5738508/

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