for object javascript code example
Example 1: javascript loop object
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
Object.keys(obj).forEach(key => {
console.log(key, obj[key]);
});
for (let key in obj) {
let value = obj[key];
console.log(key, value);
}
Example 2: js loop through object
const obj = { a: 1, b: 2 };
Object.keys(obj).forEach(key => {
console.log("key: ", key);
console.log("Value: ", obj[key]);
} );
Example 3: javascript loop through object
var obj = { first: "John", last: "Doe" };
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
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: js for in object
var obj = { foo: 'bar', baz: 42 };
console.log(Object.entries(obj));
for(const [i, val] of Object.entries(obj))
new_obj[i] = val;
Example 6: for in object javascript
var obj = { foo: 'bar', baz: 42 };
Object.keys(obj).forEach((key) => {
const el = obj[key];
console.log(
{
key: key,
value: el
}
);
});