Javascript 如何在javascript中删除二维数组中的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29070924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove row in 2d array in javascript
提问by Rajani Rampelli
How to remove row in two dimensional array in JavaScript with row number. If I want to delete all elements in row number 4 then how can do it??
如何使用行号在 JavaScript 中删除二维数组中的行。如果我想删除第 4 行中的所有元素,那怎么办??
采纳答案by Miguel Mota
Here's an example of how to remove a row by using splice:
以下是如何使用删除行的示例splice:
var array = [];
var count = 0;
for (var row=0; row<4; row++) {
array[row] = [];
for (var col=0; col<5; col++) {
array[row][col] = count++;
}
}
console.log(array);
[ [ 0, 1, 2, 3, 4 ],
[ 5, 6, 7, 8, 9 ],
[ 10, 11, 12, 13, 14 ],
[ 15, 16, 17, 18, 19 ] ]
function deleteRow(arr, row) {
arr = arr.slice(0); // make copy
arr.splice(row - 1, 1);
return arr;
}
console.log(deleteRow(array, 4));
[ [ 0, 1, 2, 3, 4 ],
[ 5, 6, 7, 8, 9 ],
[ 10, 11, 12, 13, 14 ] ]
回答by Vaibhav
Lets say you have an array 'arr' then you can remove full row by arr.splice(3,1);
假设您有一个数组 'arr' 然后您可以删除整行arr.splice(3,1);
回答by blackandorangecat
I realize this question is old, but it is one of the first results when searching for how to remove from a 2d (multidimensional) array in JS.
我意识到这个问题很老,但它是搜索如何从 JS 中的二维(多维)数组中删除时的第一个结果之一。
Here is what I used to delete the inner array based on a key of the inner array. It should continue to work if there were multiple instances of the same key. In this example, I am searching for, and removing the array with the key of 18.
这是我用来根据内部数组的键删除内部数组的内容。如果同一个密钥有多个实例,它应该继续工作。在此示例中,我正在搜索并删除键为 18 的数组。
Sorry about the formatting - it gets the point across.
对格式感到抱歉 - 它得到了重点。
var items = [
["19", 1],
["18", 2],
["20", 3]
];
//console.log(items);
document.getElementById("a").innerHTML = items;
for (var i = 0; i < items.length; i++) {
if (items[i][0] == "18") {
items.splice(i, 1);
}
}
//console.log(items);
document.getElementById("b").innerHTML = items;
<p>Before</p>
<div id='a'></div>
<p>After</p>
<div id='b'></div>
回答by gtamborero
Here you have a visual example of a bidimensional array with row deletion button (delete by ID) + jQuery preview of the table. I hope it can be usefull!
这里有一个带有行删除按钮(按 ID 删除)+ jQuery 表格预览的二维数组的可视化示例。我希望它有用!
JS DELETE ROW from Bidimensional ARRAY + Show on jQuery Cart Tablehttps://jsbin.com/xeqixi/edit?html,js,output
JS DELETE ROW from Bidimensional ARRAY + Show on jQuery Cart Tablehttps://jsbin.com/xeqixi/edit?html,js,output
回答by Rohith Gupta
delete array[index]; array.length--;
删除数组[索引]; array.length--;
In your case give index as 4 and execute the above statement and you need to manually reduce the length of array.
在您的情况下,将索引设置为 4 并执行上述语句,您需要手动减少数组的长度。
回答by simonbor
Just call the splice(4, 1)method, when 4 is row number and 1 is number of rows to remove -
只需调用该splice(4, 1)方法,当 4 是行号,1 是要删除的行数时 -
twoDimensionalArray.splice(4, 1); // remove 4th row
Also shift()and pop()are very handy methods which remove first and last rows accordingly -
还shift()和pop()是其相应地删除第一行和最后一行非常方便的方法-
twoDimensionalArray.shift(); // to remove first row
twoDimensionalArray.pop(); // to remove last row

