“Peta Object Keys JavaScript” Kode Jawaban

Peta melalui kunci javascript

var myObject = { 'a': 1, 'b': 2, 'c': 3 };

Object.keys(myObject).map(function(key, index) {
  myObject[key] *= 2;
});

console.log(myObject);
// => { 'a': 2, 'b': 4, 'c': 6 }
Bad Bat

Kunci Objek JavaScript

const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};

console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]
Kaotik

Peta melalui kunci javascript

var myObject = { 'a': 1, 'b': 2, 'c': 3 };

for (var key in myObject) {
  if (myObject.hasOwnProperty(key)) {
    myObject[key] *= 2;
  }
}

console.log(myObject);
// { 'a': 2, 'b': 4, 'c': 6 }
Bad Bat

Peta Object Keys JavaScript

const object = {1: 'a', 2: 'b', 3: 'c'};

// convert to the array keys 
let array =Object.keys(object);

// index is based on zero
let newArray = array.map((item, index) => {
    console.log(`Item => "${item}", Index => ${index}`);
    return item * 2;
});

// newArray will be [2, 4, 6]
Poor Platypus

Jawaban yang mirip dengan “Peta Object Keys JavaScript”

Pertanyaan yang mirip dengan “Peta Object Keys JavaScript”

Lebih banyak jawaban terkait untuk “Peta Object Keys JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya