javascript traverse object code example
Example 1: iterate object javascript
let obj = {
key1: "value1",
key2: "value2",
key3: "value3",
key4: "value4",
}
Object.entries(obj).forEach(([key, value]) => {
console.log(key, value);
});
Example 2: javascript loop through object
Object.entries(obj).forEach(
([key, value]) => console.log(key, value)
);
Example 3: javascript foreach object keys
var lunch = {
sandwich: 'ham',
snack: 'chips',
drink: 'soda',
desert: 'cookie',
guests: 3,
alcohol: false,
};
Object.keys(lunch).forEach(function (item) {
console.log(item);
console.log(lunch[item]);
});
Example 4: js loop over object
const object = {a: 1, b: 2, c: 3};
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
Example 5: loop through object javascript
for (var property in object) {
if (object.hasOwnProperty(property)) {
}
}
Example 6: javascript iterate through object attributes
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
}
}