Skip to content Skip to sidebar Skip to footer

How To Iterate Over Arrays And Objects In Javascript

var points = [{ x: 75, y: 25},{ x: 75+0.0046, y: 25}]; How would I iterate through this. I want to print x and y value first, then 2nd and soo on....

Solution 1:

var points = [{ x: 75, y: 25},{ x: 75+0.0046, y: 25}];

//ES5
points.forEach(function(point){ console.log("x: " + point.x + " y: " + point.y) })


//ES6
points.forEach(point=>console.log("x: " + point.x + " y: " + point.y) )

Solution 2:

You can use for..of loop.

var points = [{ x: 75, y: 25},{ x: 75+0.0046, y: 25}];
for (let point of points) {
    console.log(point.x, point.y);
}

Post a Comment for "How To Iterate Over Arrays And Objects In Javascript"