遍历 Javascript 对象属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4366104/
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 12:06:52  来源:igfitidea点击:

Traverse through Javascript object properties

javascript

提问by Rocky Singh

I want to traverse through JavaScript object's property

我想遍历 JavaScript 对象的属性

    var obj =
    {
        a: 'value1',
        b: 'value2',
        c: 'value3',
        d: 'value4'
    };

    for (var prop in obj) {
        prop = 'xxx';
    }

But the above code is not working. Can you help me how to do so ?

但是上面的代码不起作用。你能帮我怎么做吗?

回答by Christian Tellnes

You should check that the property belongs to the object and not a prototype.

您应该检查该属性是否属于对象而不是原型。

for (var prop in obj) {
    if (obj.hasOwnProperty(prop)) {
        obj[prop] = 'xxx';
    }
}

回答by Alin Purcaru

propwill reference the property name, not its value.

prop将引用属性名称,而不是它的值。

for (var prop in obj) {
    obj[prop] = 'xxx';
}

Construct documentation.

构建文档。

Also you may want to check if the property belongs to the object using hasOwnProperty. It may happen that someone adds properties to the prototype and those are also iterated by for ... in.

此外,您可能希望使用hasOwnProperty. 有人可能会向原型添加属性,并且这些属性也被for ... in.

回答by magiccrafter

Here is how it is done using the ES5 - Object.keys() :

下面是如何使用 ES5 - Object.keys() 完成它:

Object.keys(obj).forEach(function(key, idx) {
   ...
}); 

http://jsfiddle.net/magiccrafter/bvwenh5d/

http://jsfiddle.net/magiccrafter/bvwenh5d/

Mozilla's docs: link

Mozilla 的文档:链接

回答by Farid Garciayala

for(let i = 0; i < Object.entries(dog).length; i++){
  this.temp.push(Object.entries(dog)[i]);
}

回答by Snedden27

Using ecmascript2017: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

使用 ecmascript2017:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

const object1 = {
  a: 'somestring',
  b: 42
};

for (let [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

回答by veer varun singh

const obj = {
"abc":1, "def":2
}
for (let key in obj){
  console.log(key+":"+obj[key])
}

回答by relic

If you're in an ES6 friendly environment, you can also try using the for...of loop which is closer to your original attempt.

如果您处于 ES6 友好环境中,您还可以尝试使用更接近您最初尝试的 for...of 循环。

EDIT: As Caleb pointed out, for..ofis specific to collections with the Symbol.iterator property (e.g. not standard JS objects).

编辑:正如 Caleb 所指出的,for..of特定于具有 Symbol.iterator 属性的集合(例如,不是标准的 JS 对象)。

But I'm leaving this answer here in case anybody else finds it useful at some point to have it pointed out explicitly that a for..ofis not a great solution here.

但是我将这个答案留在这里,以防其他人发现在某些时候明确指出 afor..of在这里不是一个好的解决方案很有用。

let obj = {};

for (let prop of obj) { // This will throw an error
    prop = 'xxx';
}

Reference: MDN - for...of

参考:MDN - for...of