1Object.entries(obj).forEach(([key, value]) => {
2 console.log(key, value);
3});
1// Object Entries returns object as Array of [key,value] Array
2const object1 = {
3 a: 'somestring',
4 b: 42
5}
6Object.entries(object1) // Array(2) [["a", "something"], ["b", 42]]
7 .forEach(([key, value]) => console.log(`${key}: ${value}`))
8// "a: somestring"
9// "b: 42"
1The entries() method returns a new Array Iterator
2object that contains the key/value pairs for each index in the array.
1var obj = { foo: 'bar', baz: 42 };
2console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
3
4// objeto array-like
5var obj = { 0: 'a', 1: 'b', 2: 'c' };
6console.log(Object.entries(obj)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
7
8Object.entries(obj).forEach(([key, value]) => {
9 console.log(key + ' ' + value); // "a 5", "b 7", "c 9"
10});
11
1const object1 = { a: 'somestring', b: 42 };
2for (const [key, value] of Object.entries(object1)) {
3 console.log(`${key}: ${value}`);
4} // expected output: "a: somestring" "b: 42" order is not guaranteed