javascript javascript循环和删除对象属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7789014/
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
javascript looping and deleting object properties
提问by frenchie
I have an object with various properties. The name of the object is a global variable but the properties are changed at runtime by methods. Some methods add properties to the object. I'd like to add a method that loops through this object and removes all of its properties that are either null or empty. I could check each property by specifying its name and checking its state but if I add properties later, I'd have to update this cleanup method too. How can I loop through the properties of an object without know the name of the properties first.
我有一个具有各种属性的对象。对象的名称是一个全局变量,但在运行时通过方法更改属性。一些方法向对象添加属性。我想添加一个方法来循环访问此对象并删除其所有为 null 或为空的属性。我可以通过指定其名称并检查其状态来检查每个属性,但是如果我稍后添加属性,我也必须更新此清理方法。如何在不知道属性名称的情况下遍历对象的属性。
Thanks for your suggestions.
感谢您的建议。
回答by Zirak
Iteration over an object is simple - the for in
loop:
迭代对象很简单——for in
循环:
for (var key in object) {
if (object.hasOwnProperty(key)) {
//Now, object[key] is the current value
if (object[key] === null || isEmpty(object[key]))
delete object[key];
}
}
isEmpty
doesn't exist, you have to define it or replace it with something more meaningful, I couldn't understand what you meant by empty in your question.
isEmpty
不存在,您必须定义它或用更有意义的东西替换它,我无法理解您在问题中所说的空是什么意思。
I use object.hasOwnProperty
because objects inherit things from Object.prototype
and possibly other places (arrays for example inherit from Array.prototype
, which inherits from Object.prototype
). So:
我使用object.hasOwnProperty
是因为对象从Object.prototype
其他地方继承东西(例如数组继承自Array.prototype
,继承自Object.prototype
)。所以:
object.toString; //function toString() { [native code] }
But, object.toString
actually refers to Object.prototype.toString
- it isn't really in your object, but when you type object.toString
, the interpreter sees that there's no object.toString
, so it checks up the prototype chain until it finds it.
但是,object.toString
实际上是指Object.prototype.toString
- 它实际上并不在您的对象中,但是当您键入 时object.toString
,解释器会看到没有object.toString
,因此它会检查原型链,直到找到它为止。
hasOwnProperty
checks if a key actually exists on an object:
hasOwnProperty
检查对象上是否确实存在键:
object.hasOwnProperty("toString"); //false
object.foo = "bar";
object.hasOwnProperty("foo"); //true
Subscript access to objects is also simple:
对对象的下标访问也很简单:
var object = {foo: "bar"};
object.foo; //"bar"
object["foo"]; //"bar"
var key = "foo";
object[key]; //"bar"
Note that whatever is passed to the brackets gets converted to a string. So, for example, you can do this:
请注意,传递给括号的任何内容都会转换为字符串。因此,例如,您可以这样做:
object[Object.prototype.toString] = "magic";
Object.keys(object); //["function toString() { [native code] }"]
In case you're wondering, Object.keysis an ES5 (EcmaScript 5) feature.
如果您想知道,Object.keys是 ES5 (EcmaScript 5) 功能。
回答by Will
You can use a for each loop to iterate through the object properties.
您可以使用 for each 循环来遍历对象属性。
for ( var i in obj ) {
if ( obj[i] === null ) {
delete obj[i];
}
}