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??
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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.
Just call the
splice(4, 1)
method, when 4 is row number and 1 is number of rows to remove -Also
shift()
andpop()
are very handy methods which remove first and last rows accordingly -Here's an example of how to remove a row by using
splice
:Lets say you have an array 'arr' then you can remove full row by
arr.splice(3,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.
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