0

So Ive just faced this issue when my array goes broken after I delete last element from it delete array[key] and then try to push new one.

So before deleting the element it looks like this:

    guests: 
    [{ id: 263457,
        avatar: '...',
        name: 'Moamal' }],

After deleting this element it looks like this:

guests: [  ]

and after pushing new one:

    guests: 
    [ ,
     { id: 263459,
       avatar: '...',
       name: 'HalloweenD' } ]

and I'm pushing new element to this array like this

self.actives.push({ id: 263459,
       avatar: '...',
       name: 'HalloweenD' });
Sandra
  • 315
  • 3
  • 16

1 Answers1

2

Use array.splice(key,1); to delete the element instead of delete.

delete removes the element, but keeps an empty slot. Splice removes both the element and the slot.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • To be more precise: `delete` remotes the referenced property but it does not re-enumerate existing array elements since `delete` doesn't know about arrays. – Felix Kling Jan 14 '18 at 17:28