0

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?

4
  • 3
    Get rid of the inner loop. What are you wanting to do with the new Point? Commented Nov 30, 2017 at 21:59
  • Are the inner arrays coordinates (x, y)? Do you want to pass each (x, y) to the new Point? Commented Nov 30, 2017 at 22:05
  • You are actually assigning the inner arrays to the x as well as y properties.. Remove the inner for-loop and pass data[i][0] / data[i][1] to x/y. Also it would be helpful if you would show us how Point() is implemented Commented Nov 30, 2017 at 22:07
  • I did but still the X Y values are empty Commented Nov 30, 2017 at 22:08

2 Answers 2

3
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

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

4 Comments

Can you provide more code for the Point constructor?
The documentation specifies that you need SpatialReference appart from the coordinates x and y. Also where are you using the new object point, outside the loop?
Give it a try even with this piece of code: let point = new Point(data[i])
2

Quick and simple:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.