xxxxxxxxxx
const obj = {
key1: 'value1',
key2: 'value2',
key3: 'value3'
};
// Iterate over the object using for...in loop
for (let key in obj) {
// Access the value of each key
const value = obj[key];
console.log(key + ': ' + value);
}
xxxxxxxxxx
var obj = { foo: 'bar', baz: 42 };
Object.keys(obj).forEach((key) => {
const el = obj[key];
console.log(
{
key: key,
value: el
}
);
});
xxxxxxxxxx
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;
xxxxxxxxxx
const obj = { a: 1, b: 2, c: 3 };
for (let key in obj) {
console.log(key + ": " + obj[key]);
}