0

UPD:
What I really should use to remove some objects from another object?
Is data[ 'some_key' ] = undefined good enough?

ORIGINAL QUESTION:
I want to remove some objects that are stored in another object, some like that:

var data = {
    'a': {...},
    'b': {...},
    'c': {...},
    ...
}

Is it true that the use of "delete" operator is not a really good practice?
When I should and shouldn't use it?

Legotin
  • 2,378
  • 1
  • 18
  • 28

1 Answers1

-1

Deleting elements in arrays, creates holes (the length property is not updated)

> var arr = [ 'a', 'b' ];
> arr.length
2
> delete arr[1]  // does not update length
true
> arr
[ 'a',  ]
> arr.length
2

You can also delete trailing array elements by decreasing an array’s length.

Delete is the only true way to remove object's properties without any leftovers, but it works ~ 100 times slower, compared to it's "alternative", setting object[key] = undefined.

koninos
  • 4,969
  • 5
  • 28
  • 47