create object in loop javascript code example
Example 1: loop through object javascript
var p = {
"p1": "value1",
"p2": "value2",
"p3": "value3"
};
for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}
for (var key of Object.keys(p)) {
console.log(key + " -> " + p[key])
}
for (let [key, value] of Object.entries(p)) {
console.log(`${key}: ${value}`);
}
Example 2: how to loop object javascript
var obj = {a: 1, b: 2, c: 3};
for (const prop in obj) {
console.log(`obj.${prop} = ${obj[prop]}`);
}
Example 3: loop through object js
var obj = {
first: "John",
last: "Doe"
};
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
Example 4: how to loop trough an object java script
const keys = Object.keys(fruits)
for (const key of keys) {
console.log(key)
}
Example 5: object for loop
const object = {a: 1, b: 2, c: 3};
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
Example 6: how to loop trough an object java script
const fruits = {
apple: 28,
orange: 17,
pear: 54,
}
const values = Object.values(fruits)
console.log(values)