我有一个这样的对象:
var names = { 45: "Jeff", 145: "Peter", 11: "Dandie", 879: "Michael" }
如何从对象中删除“彼得”?
我有一个这样的对象:
var names = { 45: "Jeff", 145: "Peter", 11: "Dandie", 879: "Michael" }
如何从对象中删除“彼得”?
尝试这个
delete names['145'];
或者
delete names.145;
该代码是不好的做法,键和值的顺序错误。尝试以下方法来实现您想要的:
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];
});