Sunday, May 19, 2024
190
rated 0 times [  194] [ 4]  / answers: 1 / hits: 41117  / 9 Years ago, mon, march 16, 2015, 12:00:00

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??


More From » multidimensional-array

 Answers
3

Here's an example of how to remove a row by using 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 ] ]

[#67428] Thursday, March 12, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckaylab

Total Points: 311
Total Questions: 120
Total Answers: 93

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
;