To loop through an array containing objects and access their properties with JavaScript, we use the forEach
method.
For instance, we write
yourArray.forEach((arrayItem) => {
const x = arrayItem.prop + 2;
console.log(x);
});
to call forEach
with a callback that gets the prop
property value from the arrayItem
being looped through.
And then we add 2 to it assign the sum to x
.