Mona Coder
Mona Coder

Reputation: 6316

Unable to loop over array in array

I have an array of arrays which looks like this:

   var data =  [
      [
        -9814184.757,
        5130582.574600004
      ],
      [
        -9814152.5879,
        5130624.636799999
      ],
      [
        -9814147.7353,
        5130632.882600002
      ]
    ]

Now when I try to map it into an object like

for (i = 0; i < data.length; ++i) {
    for (b = 0; b < 1; ++b) {
        var point = new Point(data[i][b],data[i][b]);
    }
}
console.log(point);

I am getting undefined for x and y in the object

{type: "point", x: undefined, y: undefined, spatialReference: {…}}

What am I doing wrong?

Upvotes: 0

Views: 55

Answers (2)

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94319

Quick and simple:

points = data.map(([x, y]) => new Point(x, y));

Upvotes: 2

rob_
rob_

Reputation: 144

for (let i = 0; i < data.length; i++) {
    let point = new Point(data[i][0], data[i][1]);
    console.log(point);
}

Loop through the array called data in your case. For each of the inner arrays read the first item and assign it to x value and the second item to y value

Upvotes: 3

Related Questions