javascript loop object array code example
Example 1: javascript loop object
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
Object.keys(obj).forEach(key => {
console.log(key, obj[key]);
});
for (let key in obj) {
let value = obj[key];
console.log(key, value);
}
Example 2: object loop in javascript
const object = { a: 1, b: 2, c: 3 };
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
Example 3: javascript loop through object example
var person={
first_name:"johnny",
last_name: "johnson",
phone:"703-3424-1111"
};
for (var property in person) {
console.log(property,":",person[property]);
}
Example 4: javascript object array iteration
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
Object.keys(obj).forEach(key => {
let value = obj[key];
});
Example 5: loop array of objects
const myArray = [{x:100}, {x:200}, {x:300}];
const newArray= myArray.map(element => element.x);
console.log(newArray);
Example 6: iterate over array of object javascript and access the properties
for (let item of items) {
console.log(item);
}