add items to arraylist in js foreach code example

Example 1: javascript foreach call specific value in array

run.addEventListener("click", function () {
    people.forEach((element) => console.log(element.firstname));
  }); // select a specific string in array

Example 2: 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); // 600 = 0 + 100 + 200 + 300

const average = sum / myArray.length;
console.log(average); // 200