Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

javascript - 如何从javascript关联数组中删除对象?(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() ?(myArray["lastname"].remove() ?) (I need the element gone because the number of elements is important and I want to keep things clean.)((我需要元素消失,因为元素的数量很重要,我想保持清洁。))   ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the "delete" keyword in Javascript.(在Javascript中使用“delete”关键字。)

delete myArray["lastname"]; EDIT:(编辑:) In some JavaScript engine, the delete keyword might hurt performance as it will undo compile / JIT optimization.(在某些JavaScript引擎中,delete关键字可能会损害性能,因为它会撤消编译/ JIT优化。) http://www.html5rocks.com/en/tutorials/speed/v8/ http://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/(http://www.html5rocks.com/en/tutorials/speed/v8/ http://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...