-2

我有一个这样的对象:

var names = { 45: "Jeff", 145: "Peter", 11: "Dandie", 879: "Michael" }

如何从对象中删除“彼得”?

4

2 回答 2

0

尝试这个

delete names['145'];

或者

delete names.145;
于 2013-07-21T16:16:47.070 回答
-1

该代码是不好的做法,键和值的顺序错误。尝试以下方法来实现您想要的:

for(var key of Object.keys(names))
    if(names[key]=='John')
         delete names[key];

没有 for .. of:

Object.keys(names).forEach(function(key){
    if(names[key]=='John')
         delete names[keys];
});
于 2013-07-21T16:23:35.413 回答