0

I just deleted an element in my array using this code:

delete chckboxIDs[0][0];

This is what the data in my array looks like now:

enter image description here

Now my array has an empty cell which breaks the code because i loop through it based on its length in other places in my program causing a null value exception. How do i get rid of this empty space?

If i shift all the elements down until it was in the last position wouldnt it still be there and still break the code? Is there a better way to do this?

Some_Dude
  • 199
  • 2
  • 18
  • None of these are removing elements from a multidimensional array. – Some_Dude Jun 28 '18 at 14:48
  • I tried chckboxIDs.splice(chckboxIDs[0][0], 1); to remove the first element, but it deleted all of them. – Some_Dude Jun 28 '18 at 14:53
  • The array element is an array. Using `Array.prototype.splice` on the nested array gives the result you asked for. I'd suggest reading up on [splice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice). – Boris Jun 28 '18 at 14:53

1 Answers1

9

You try using splice function, delete is not really advisable in removing element in an array or 2d array in Javascript

chckboxIDs[row].splice(col, 1);
onecompileman
  • 809
  • 4
  • 11