“Entri objek JavaScript” Kode Jawaban

untuk nilai kunci dalam javascript objek

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}
Smoggy Shark

Entri objek JavaScript

// 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"
POG

Entri objek JS

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"
});
GutoTrosla

Object.Entries dalam JavaScript

// 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' ]
]
**/
Chetan Nada

Jawaban yang mirip dengan “Entri objek JavaScript”

Pertanyaan yang mirip dengan “Entri objek JavaScript”

Lebih banyak jawaban terkait untuk “Entri objek JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya