按值删除属性

时间:2013-07-21 16:08:02

标签: javascript

我有一个这样的对象:

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

如何从对象中删除“Peter”?

2 个答案:

答案 0 :(得分:0)

试试这个

delete names['145'];

delete names.145;

答案 1 :(得分:-1)

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

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

没有...:

Object.keys(names).forEach(function(key){
    if(names[key]=='John')
         delete names[keys];
});