iterate array of object and calculate value in javascript code example

Example 1: iterate through object array javascript

for (var key in array) {
    var obj = myArray[key];
    // ...
}

Example 2: iterate over array of objects javascript

// Create a new array based on the original but without modifying it

const myArray = [{x:100}, {x:200}, {x:300}];

const newArray= myArray.map(element => {
    return {
        ...element,
        x: element.x * 2
    };
});

console.log(myArray); // [100, 200, 300]
console.log(newArray); // [200, 400, 600]