变量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如何表示对象的财产?它不是内置方法或属性。为什么它会产生对象中的每个属性?
当前回答
你可以使用Lodash。文件
var obj = {a: 1, b: 2, c: 3};
_.keys(obj).forEach(function (key) {
...
});
其他回答
这是为了。。。in语句(MDN、ECMAScript规范)。
您可以将其读为“对于obj对象中的每个属性,依次将每个属性分配给PROPT变量”。
为何在循环中,它创建了一个新变量(varsomeVariable),然后将给定对象的每个属性逐一存储在这个新变量(someVariable)中。因此,如果使用块{},则可以进行迭代。考虑以下示例。
var obj = {
name:'raman',
hobby:'coding',
planet:'earth'
};
for(var someVariable in obj) {
//do nothing..
}
console.log(someVariable); // outputs planet
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]);
});
}
如果您的环境支持ES2017,那么我将推荐Object.entries:
Object.entries(obj).forEach(([key, value]) => {
console.log(`${key} ${value}`);
});
如Mozillas Object.entries()文档所示:
Object.entries()方法返回给定对象自身的数组可枚举属性[key,value]对,顺序与由…提供。。。in循环(区别在于for in循环还列举了原型链中的财产)。
基本上,使用Object.entries,我们可以放弃旧的for。。。循环中:
// This step is not necessary with Object.entries
if (object.hasOwnProperty(property)) {
// do stuff
}
添加ES2015对Reflect.ownKeys(obj)的使用,并通过迭代器迭代财产。
例如:
let obj = { a: 'Carrot', b: 'Potato', Car: { doors: 4 } };
可以通过
// logs each key
Reflect.ownKeys(obj).forEach(key => console.log(key));
如果您想直接迭代对象的键的值,可以定义迭代器,就像JavaScipts的字符串、数组、类型化数组、Map和Set的默认迭代器一样。
JS将尝试通过默认迭代器属性进行迭代,该属性必须定义为Symbol.iterator。
如果希望能够遍历所有对象,可以将其添加为Object的原型:
Object.prototype[Symbol.iterator] = function*() {
for(p of Reflect.ownKeys(this)){ yield this[p]; }
}
这将使您能够使用for。。。例如:
for(val of obj) { console.log('Value is:' + val ) }
注意:截至撰写此答案(2018年6月),除IE外,所有其他浏览器都支持生成器和。。。通过Symbol.iterator迭代