如何从一个javascript关联数组删除对象? [英] How do I remove objects from a javascript associative array?
问题描述
假设我有这样的code:
Suppose I have this code:
var myArray = new Object();
myArray["firstname"] = "Bob";
myArray["lastname"] = "Smith";
myArray["age"] = 25;
现在,如果我想删除姓氏?......有没有结果的一些等价 myArray的[姓氏。删除()
?
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.)
推荐答案
使用删除关键字在Javascript。
Use the "delete" keyword in Javascript.
delete myArray["lastname"];
编辑:
在一些JavaScript引擎,删除关键字可能会伤害性能,因为它会撤消编译/ JIT优化。
In some JavaScript engine, the delete keyword might hurt performance as it will undo compile / JIT optimization.
<一个href=\"http://www.html5rocks.com/en/tutorials/speed/v8/\">http://www.html5rocks.com/en/tutorials/speed/v8/
<一href=\"http://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/\">http://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/
这篇关于如何从一个javascript关联数组删除对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!