简体   繁体   English

如何从阵列中删除数组?

[英]How do I remove an array from an array?

Here is my array: 这是我的数组:

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"]]

How do I remove the second element ["add", null, ""] so that the new array is 如何删除第二个元素["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"]]

That element might not always be in the second position but its first value will always be "add". 该元素可能并不总是处于第二位置,但其第一个值始终为“add”。 How do I remove the array with the first value (myarray[1][0]) of "add"? 如何使用“add”的第一个值(myarray[1][0])删除数组?

That element might not always be in the second position but its first value will always be "add". 该元素可能并不总是处于第二位置,但其第一个值始终为“add”。 How do I remove the array with the first value (myarray[1][0]) of "add"? 如何使用“add”的第一个值(myarray[1][0])删除数组?

Use a loop with splice() . 使用带splice()的循环。

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

jsFiddle . jsFiddle

Use splice , like so: 使用splice ,如下所示:

myarray.splice(1, 1)

See this tutorial for more information on splice . 有关splice更多信息,请参阅本教程

You can remove it completely (slow): 你可以完全删除它(慢):

myarray.splice(1, 1);

You can delete it from the array, and left a "hole" in it (left the position 1 undefined): 你可以从数组中删除它,并在其中留下一个“洞”(左边的位置1未定义):

delete myarray[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM