array of object how to do a for loop code example
Example 1: loop array of objects
const myArray = [{x:100}, {x:200}, {x:300}];
const newArray= myArray.map(element => element.x);
console.log(newArray);
Example 2: loop array of objects
const myArray = [{x:100}, {x:200}, {x:300}];
const newArray= myArray.map(element => {
return {
...element,
x: element.x * 2
};
});
console.log(myArray);
console.log(newArray);
Example 3: how to get value in array object value using for loop in javascript
const myArray = [{x:100}, {x:200}, {x:300}];
const sum = myArray.map(element => element.x).reduce((a, b) => a + b, 0);
console.log(sum);
const average = sum / myArray.length;
console.log(average);
Example 4: iterate over array of object javascript and access the properties
for (let item of items) {
console.log(item);
}