Para o valor -chave no objeto JavaScript
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
Smoggy Shark
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
// Object Entries returns object as Array of [key,value] Array
const object1 = {
a: 'somestring',
b: 42
}
Object.entries(object1) // Array(2) [["a", "something"], ["b", 42]]
.forEach(([key, value]) => console.log(`${key}: ${value}`))
// "a: somestring"
// "b: 42"
var obj = { foo: 'bar', baz: 42 };
console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
// objeto array-like
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.entries(obj)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
Object.entries(obj).forEach(([key, value]) => {
console.log(key + ' ' + value); // "a 5", "b 7", "c 9"
});
// Object.entries() in javascript
const credits = { producer: 'John', director: 'Jane', assistant: 'Peter' };
const arr = Object.entries(credits);
console.log(arr);
/** Output:
[ [ 'producer', 'John' ],
[ 'director', 'Jane' ],
[ 'assistant', 'Peter' ]
]
**/