45
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....

Alex Pan's user avatar
Alex Pan
4,7618 gold badges39 silver badges46 bronze badges
askedNov 4, 2016 at 6:10
gaurav singh's user avatar
1

3 Answers3

14

UseArray#forEach method for array iteration.

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

Sign up to request clarification or add additional context in comments.

2 Comments

if i want to store the data in some var than ..var X=obj.x; var Y=obj.y; this can be done or not since m new in js;
@gauravsingh this will give you only last value as you are assigning values in a loop. Please explain what you are trying to achieve and then it would be easier for us to help you. Also, next time, please refer to suggestion you get when you are asking question
4

var points = [{ x: 75, y: 25},{ x: 75+0.0046, y: 25}];//ES5points.forEach(function(point){ console.log("x: " + point.x + " y: " + point.y) })//ES6points.forEach(point=> console.log("x: " + point.x + " y: " + point.y) )

answeredNov 4, 2016 at 6:13
A.T.'s user avatar

1 Comment

This is a very basic question that has already been answer few times. You should mark it as duplicate
2

You can usefor..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);}

answeredNov 4, 2016 at 6:14
Rax Weber's user avatar

2 Comments

I typed the answer faster than searching for the duplicate and flagging it as such.
I must say you have some typing speed, but its better to keepour portal clean. Also remember, Old posts have more answers covering more options , so person searching for it will help him learn more if you mark as duplicate

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.