const object = { a: 1, b: 2, c: 3 };
// method 1
Object.entries(object).forEach(([key, value]) => {
console.log(key, value)
});
//method 2
for (const key in object) {
console.log(key, object[key])
}
// same output
// a 1
// b 2
// c 3
var obj = { foo: 'bar', baz: 42 };
Object.keys(obj).forEach((key) => {
const el = obj[key];
console.log(
{
key: key,
value: el
}
);
});
var obj = { foo: 'bar', baz: 42 };
console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
for(const [i, val] of Object.entries(obj))
new_obj[i] = val;
for (key in object) {
console.log(key,object[key]);
}
// an easy way is to
const obj = {1: 'a', 2: 'b', 3: 'c',};
for (const i in obj)
console.log(obj[i]);
//it is a simple way to loop on values of an object