How to iterate over arrays and objects in JavaScript

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

How would I try this. I want to print the values โ€‹โ€‹of x and y first, then the 2nd and soo on ....

+5
source share
3 answers

Use the Array#forEach method to iterate through the array.

 var points = [{ x: 75, y: 25 }, { x: 75 + 0.0046, y: 25 }]; points.forEach(function(obj) { console.log(obj.x, obj.y); }) 
+5
source

 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) ) 
+2
source

You can use the for..of .

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

Source: https://habr.com/ru/post/1259262/


All Articles