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 ?
propwill reference the property name, not its value.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 byfor ... in.