14

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

Anthon
  • 69,918
  • 32
  • 186
  • 246
Rajani Rampelli
  • 179
  • 1
  • 1
  • 12

6 Answers6

17

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 ] ]
Miguel Mota
  • 20,135
  • 5
  • 45
  • 64
5

Lets say you have an array 'arr' then you can remove full row by arr.splice(3,1);

Vaibhav
  • 1,479
  • 8
  • 13
  • What is 3.In this case?? row -1?? – Rajani Rampelli Mar 16 '15 at 09:23
  • It is specific to your question, you mentioned 4th element. So if you want to removed nth row then use arr.splice(n-1,1). Here first parameter is the index of the row, and second parameter for how many element you want to remove. In your case of 2D array, it means how many rows you want to delete. – Vaibhav Mar 16 '15 at 09:26
  • what are you passing as first parameter? – Vaibhav Mar 16 '15 at 09:41
  • This code deleting the last row. If array have 4th row is getting deleted. If 6rows array have. 6th row will get deleted. – Rajani Rampelli Mar 16 '15 at 09:42
  • In your case, row will be having total number of rows, then only your last row would be deleted. See this link. http://jsbin.com/qopizojide/1/ – Vaibhav Mar 16 '15 at 09:47
  • Choose it as best answer if it helped :) – Vaibhav Mar 16 '15 at 09:50
3

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.

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.

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>
blackandorangecat
  • 1,246
  • 5
  • 18
  • 37
2

Just call the splice(4, 1) method, when 4 is row number and 1 is number of rows to remove -

twoDimensionalArray.splice(4, 1); // remove 4th row

Also shift() and pop() are very handy methods which remove first and last rows accordingly -

twoDimensionalArray.shift(); // to remove first row
twoDimensionalArray.pop(); // to remove last row
Simon Borsky
  • 4,979
  • 2
  • 22
  • 20
1

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!

JS DELETE ROW from Bidimensional ARRAY + Show on jQuery Cart Table https://jsbin.com/xeqixi/edit?html,js,output

gtamborero
  • 2,898
  • 27
  • 28
-1

delete array[index]; array.length--;

In your case give index as 4 and execute the above statement and you need to manually reduce the length of array.

  • Manually adjusting the `length` property is a red flag. Your code will leave an empty item in index 4, and you will lose the last item in the array. – Théophile Jul 21 '22 at 05:42