简体   繁体   English

无法遍历数组中的数组

[英]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 我在对象中的x和y上undefined

{type: "point", x: undefined , y: undefined , spatialReference: {…}} {type:“ point”,x: 未定义 ,y: 未定义 ,空间参考:{…}}

What am I doing wrong? 我究竟做错了什么?

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 对于每个内部数组,请读取第一个项目并将其分配给x值,第二个项目分配给y值

快速简单:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM