Traverse through Javascript object properties

Javascript

Javascript Problem Overview


I want to traverse through JavaScript object's property

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 ?

Javascript Solutions


Solution 1 - Javascript

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';
    }
}

Solution 2 - Javascript

prop will reference the property name, not its value.

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.

Solution 3 - Javascript

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

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

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

Mozilla's docs: link

Solution 4 - Javascript

Using 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}`);
}

Solution 5 - Javascript

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

Solution 6 - Javascript

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

Solution 7 - Javascript

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

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

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..of is not a great solution here.

let obj = {};

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

Reference: MDN - for...of

Solution 8 - Javascript

var temp= {"6s","vikash","500"};
console.log([...temp]); //["6s","vikash","500"]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRocky SinghView Question on Stackoverflow
Solution 1 - JavascriptChristian TellnesView Answer on Stackoverflow
Solution 2 - JavascriptAlin PurcaruView Answer on Stackoverflow
Solution 3 - JavascriptmagiccrafterView Answer on Stackoverflow
Solution 4 - JavascriptSnedden27View Answer on Stackoverflow
Solution 5 - JavascriptFarid GarciayalaView Answer on Stackoverflow
Solution 6 - Javascriptuser7854158View Answer on Stackoverflow
Solution 7 - JavascriptrelicView Answer on Stackoverflow
Solution 8 - Javascriptvikash vishwakarmaView Answer on Stackoverflow