1
delete addon_categories[index];

If index is 1, this statement leaves an undefined placeholder in the array.

[{Object}, undefined]

How do I solve this?

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
anonym
  • 4,460
  • 12
  • 45
  • 75
  • Did you look at this question? https://stackoverflow.com/questions/500606/deleting-array-elements-in-javascript-delete-vs-splice – Colin Jul 27 '17 at 05:32
  • In that case use Array.splice method.SHare your array for more insight – brk Jul 27 '17 at 05:33
  • 3
    Possible duplicate of [Deleting array elements in JavaScript - delete vs splice](https://stackoverflow.com/questions/500606/deleting-array-elements-in-javascript-delete-vs-splice) – Thamilhan Jul 27 '17 at 05:35
  • delete will delete the object property, but will not reindex the array or update its length. So for your case `splice(startIndex, itemsToDelete)` is what you need. – Manish Jul 27 '17 at 05:36
  • This is expected behavior. – Avnesh Shakya Jul 27 '17 at 05:36
  • just use array.splice for this specific case, likely `addon_categories.splice(index, 1)`, where I currently suppose that index is 1. – briosheje Jul 27 '17 at 05:37
  • All you're doing is deleting the value of that element of the array, not the element itself. – Clonkex Jul 27 '17 at 06:14

1 Answers1

1

Try this:

if (index > -1) {
   addon_categories.splice(index, 1);
}

Using delete keyword, it will set undefined, will not remove the element complete it's expected behavior.

Avnesh Shakya
  • 3,828
  • 2
  • 23
  • 31