变量obj={name:“西蒙”,年龄:“20”,服装:{style:“简单”,嬉皮士:假}}for(obj中的var propt){console.log(propt+':'+obj[propt]);}
变量propt如何表示对象的财产?它不是内置方法或属性。为什么它会产生对象中的每个属性?
变量obj={name:“西蒙”,年龄:“20”,服装:{style:“简单”,嬉皮士:假}}for(obj中的var propt){console.log(propt+':'+obj[propt]);}
变量propt如何表示对象的财产?它不是内置方法或属性。为什么它会产生对象中的每个属性?
当前回答
为了进一步完善接受的答案,值得注意的是,如果使用var object=object.create(null)实例化对象,那么object.hasOwnProperty(property)将触发TypeError。所以为了安全起见,您需要从原型中调用它,如下所示:
for (var property in object) {
if (Object.prototype.hasOwnProperty.call(object, property)) {
// do stuff
}
}
其他回答
如果运行Node,我建议:
Object.keys(obj).forEach((key, index) => {
console.log(key);
});
如果您只想迭代映射属性值,则lodash具有_.mapValues
常量对象={a: 2,b: 3个}常量res=_.map值(obj,v=>v*2)console.log(res)<script src=“https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js“></script>
在这里,我迭代每个节点并创建有意义的节点名称。如果您注意到,instanceOfArray和instanceOfObject几乎做了相同的事情(在我的应用程序中,我给出了不同的逻辑)
function iterate(obj,parent_node) {
parent_node = parent_node || '';
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
var node = parent_node + "/" + property;
if(obj[property] instanceof Array) {
//console.log('array: ' + node + ":" + obj[property]);
iterate(obj[property],node)
} else if(obj[property] instanceof Object){
//console.log('Object: ' + node + ":" + obj[property]);
iterate(obj[property],node)
}
else {
console.log(node + ":" + obj[property]);
}
}
}
}
注:我受到了翁德雷·斯文达尔(Ondrej Svejdar)的回答的启发。但此解决方案具有更好的性能和更少的模糊性
if (typeof obj === 'object' && obj !== null) {
Object.keys(obj).forEach(key => {
console.log("\n" + key + ": " + obj[key]);
});
}
// *** Explanation line by line ***
// Explaining the bellow line
// It checks if obj is neither null nor undefined, which means it's safe to get its keys.
// Otherwise it will give you a "TypeError: Cannot convert undefined or null to object" if obj is null or undefined.
// NOTE 1: You can use Object.hasOwnProperty() instead of Object.keys(obj).length
// NOTE 2: No need to check if obj is an array because it will work just fine.
// NOTE 3: No need to check if obj is a string because it will not pass the 'if typeof obj is Object' statement.
// NOTE 4: No need to check if Obj is undefined because it will not pass the 'if type obj is Object' statement either.
if (typeof obj === 'object' && obj !== null) {
// Explaining the bellow line
// Just like in the previous line, this returns an array with
// all keys in obj (because if code execution got here, it means
// obj has keys.)
// Then just invoke built-in javascript forEach() to loop
// over each key in returned array and calls a call back function
// on each array element (key), using ES6 arrow function (=>)
// Or you can just use a normal function ((key) { blah blah }).
Object.keys(obj).forEach(key => {
// The bellow line prints out all keys with their
// respective value in obj.
// key comes from the returned array in Object.keys(obj)
// obj[key] returns the value of key in obj
console.log("\n" + key + ": " + obj[key]);
});
}
迭代财产需要额外的hasOwnProperty检查:
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
// do stuff
}
}
这是必要的,因为对象的原型包含对象的附加财产,这些属性在技术上是对象的一部分。这些附加的财产继承自基本对象类,但仍然是obj的财产。
hasOwnProperty只是检查这是否是该类特有的属性,而不是从基类继承的属性。
也可以通过对象本身调用hasOwnProperty:
if (obj.hasOwnProperty(prop)) {
// do stuff
}
但如果对象具有同名的不相关字段,则此操作将失败:
var obj = { foo: 42, hasOwnProperty: 'lol' };
obj.hasOwnProperty('foo'); // TypeError: hasOwnProperty is not a function
这就是为什么通过Object.prototype调用它更安全的原因:
var obj = { foo: 42, hasOwnProperty: 'lol' };
Object.prototype.hasOwnProperty.call(obj, 'foo'); // true