“Lingkarkan melalui objek JavaScript” Kode Jawaban

Loop JavaScript melalui nilai objek

var myObj = {foo: "bar", baz: "baz"};
Object.values(myObj).map((val) => {
console.log(val);
})
// "bar" "baz"
Thoughtful Tamarin

JavaScript Iterate Over Object

var obj = { first: "John", last: "Doe" };

Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
});
Bald Eagle

Lingkarkan melalui objek JavaScript

for (var property in object) {
  if (object.hasOwnProperty(property)) {
    // Do things here
  }
}
Bald Eagle

Lingkarkan melalui objek JavaScript

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

// for-in
for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}

// for-of with Object.keys()
for (var key of Object.keys(p)) {
    console.log(key + " -> " + p[key])
}

// Object.entries()
for (let [key, value] of Object.entries(p)) {
  console.log(`${key}: ${value}`);
}
Web Surfer

Lingkarkan melalui objek JavaScript

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}
MunchDuster

Lingkarkan melalui objek JavaScript

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}
Motionless Meerkat

Jawaban yang mirip dengan “Lingkarkan melalui objek JavaScript”

Pertanyaan yang mirip dengan “Lingkarkan melalui objek JavaScript”

Lebih banyak jawaban terkait untuk “Lingkarkan melalui objek JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya