Possible Duplicates:
How can I unset a JavaScript variable?
How do I remove a property from a JavaScript object?
I’m looking for a way to remove/unset the properties of a JavaScript object, so they’ll no longer come up if I loop through the object doing for (var i in myObject). How can this be done?
Simply use
delete, but be aware that you should read fully what the effects are of using this:But if I was to use like so:
But if you do wish to delete variables in the global namespace, you can use its global object such as
window, or usingthisin the outermost scope, i.e.,Understanding delete
Another fact is that using delete on an array will not remove the index, but only set the value to undefined, meaning in certain control structures such as for loops, you will still iterate over that entity. When it comes to arrays you should use
splicewhich is a prototype of the array object.Example Array:
If I was to do:
the resulting array would be:
But using splice like
would result in: