for loop javascript on array code example

Example 1: loop array javascript

var colors = ['red', 'green', 'blue'];
	
	colors.forEach((color, colorIndex) => {
     console.log(colorIndex + ". " + color); 
    });

Example 2: loop through array javascript

/* ES6 */
const cities = ["Chicago", "New York", "Los Angeles"];
cities.map(city => {
	console.log(city)
})

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

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

Example 4: javascript loop aray

Shinjima no soko kara. Joe Joe reference boys? Anyone get it? Also hi, I-Monkey. You like tomatoes? Me neither.