Javascript 如何从对象中删除属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4178123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:16:01  来源:igfitidea点击:

How to remove a property from an object?

javascript

提问by Rachel

Currently an event is set on checkboxes, and event.targetgives me the status (checked = true/false)of checkbox which is clicked.

当前在 上设置了一个事件checkboxes,并event.target为我提供了status (checked = true/false)单击的复选框。

I am maintaining an object which keeps the track on all the selected checkboxes

我正在维护一个对象,该对象将跟踪所有选定的复选框

var selectedMap  = {};

if(event.target == true){
    var key = event.target.id;
    var val = event.target.name;
    selectedMap[key] = val;
}

and I want to remove the element from the map which is unselected

我想从地图中删除未选中的元素

else if(event.target == false){
  selectedMap.remove(event.target.id);
}

when I run this it gives me error in Firebug: selectedMap.remove is not a function

当我运行它时,它给了我以下错误FirebugselectedMap.remove is not a function

So my question is How can I remove the element when the checkbox is unselected ?

所以我的问题是如何在未选中复选框时删除元素?

回答by T.J. Crowder

Using delete:

使用delete

delete selectedMap[event.target.id];

You're setting the value incorrectly, though. Here's the correct way:

但是,您设置的值不正确。这是正确的方法:

if(event.target == true){
    var key = event.target.id;   // <== No quotes
    var val = event.target.name; // <== Here either
    selectedMap[key] = val;
}

In fact, you could:

事实上,你可以:

if(event.target == true){
    selectedMap[event.target.id] = event.target.name;
}

Getting the event target stuff out of the way, it's easier to envision this with simple strings:

去掉事件目标的东西,用简单的字符串更容易想象这一点:

var obj = {};
obj.foo = "value of foo";
alert(obj.foo);    // alerts "value of foo" without the quotes
alert(obj["foo"]); // ALSO alerts "value of foo" without the quotes, dotted notation with a literal and bracketed notation with a string are equivalent
delete obj.foo;    // Deletes the `foo` property from the object entirely
delete obj["foo"]; // Also deletes the `foo` property from the object entirely
var x = "foo";
delete obj[x];     // ALSO deeltes the `foo` property

When using a plain object like this, I always use a prefix on my keys to avoid issues. (For instance, what would happen if your target element's ID was "toString"? The object already has an [inherited] property called "toString" and things would get Very Weird Very Quickly.)

当使用像这样的普通对象时,我总是在我的键上使用前缀以避免出现问题。(例如,如果你的目标元素的 ID 是“toString”会发生什么?该对象已经有一个名为“toString”的[继承]属性,事情会很快变得非常奇怪。)

So for me, I do this:

所以对我来说,我这样做:

if(event.target == true){
    selectedMap["prefix" + event.target.id] = event.target.name;
}

...and of course:

...而且当然:

delete selectedMap["prefix" + event.target.id];

回答by Russ Cam

What you have is an object and not an array (although an array is an object). You declare an object literal with {}whereas an array literal is declared with [].

你拥有的是一个对象而不是一个数组(尽管数组是一个对象)。您使用 声明对象文字,{}而使用 声明数组文字[]

You can use deleteto remove an object property like so

您可以delete像这样删除对象属性

delete selectedMap[event.target.id];