1

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
  • 309
  • 5
  • 21
  • 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

12

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
  • 900
  • 7
  • 14
  • 1
    This works! Thanks. That makes sense now. Couldn't figure out how to adapt the splice code I've seen in other posts for regular arrays into a multidimensional format. – Some_Dude Jun 28 '18 at 14:56