Loop JavaScript melalui nilai objek
var myObj = {foo: "bar", baz: "baz"};
Object.values(myObj).map((val) => {
console.log(val);
})
// "bar" "baz"
Thoughtful Tamarin
var myObj = {foo: "bar", baz: "baz"};
Object.values(myObj).map((val) => {
console.log(val);
})
// "bar" "baz"
for (var property in object) {
if (object.hasOwnProperty(property)) {
// Do things here
}
}
const object = { a: 1, b: 2, c: 3 };
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
const object = {a: 1, b: 2, c: 3};
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
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}`);
}
var obj ={ name: "hery" ,age =14};
for(var p in obj){
if(obj.hasOwnProperty(p){
console.log(p +"->"+ p[key]);
}
}