Warning
for...in is intended for iterating over object keys, not array indexes. Using it to loop through an array is generally discouraged. It also includes properties from the prototype, so it may be necessary to check if the key is within the object usinghasOwnProperty
. If any attributes in the object are defined by thedefineProperty/defineProperties
method and set the paramenumerable: false
, those attributes will be inaccessible.
var object = {"a":"foo", "b":"bar", "c":"baz"};
// `a` is inaccessible
Object.defineProperty(object , 'a', {
enumerable: false,
});
for (var key in object) {
if (object.hasOwnProperty(key)) {
console.log('object.' + key + ', ' + object[key]);
}
}
Expected output:
object.b, bar
object.c, baz