Wednesday, 14 March 2018

How do I remove objects from a javascript associative array?



Suppose I have this code:



var myArray = new Object();
myArray["firstname"] = "Bob";
myArray["lastname"] = "Smith";
myArray["age"] = 25;



Now if I wanted to remove "lastname"?....is there some equivalent of
myArray["lastname"].remove()?



(I need the element gone because the number of elements is important and I want to keep things clean.)


Answer



Use the "delete" keyword in Javascript.



delete myArray["lastname"];






EDIT:



In some JavaScript engine, the delete keyword might hurt performance as it will undo compile / JIT optimization.



http://www.html5rocks.com/en/tutorials/speed/v8/
http://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...