-3

我想输入一个数组数组,然后将它们分开。因此,例如,我有一个位置数组,例如多个纬度和经度坐标。但我想编写一个循环,然后将获取该数组数组并为所有纬度坐标和所有经度坐标生成和数组。

例如,如果我有

input = [[45,45],[35,75][85,90]] 它将生成 2 个数组作为我的输出 [45,35,85] 和 [45,75,90]

4

3 回答 3

0

您可以转置数组并将latlong作为单个数组。

var input = [[45, 45], [35, 75], [85, 90]],
    [lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || []).concat(v)), []);
    
console.log(lat);
console.log(long);
.as-console-wrapper { max-height: 100% !important; top: 0; }

于 2018-11-08T18:44:29.380 回答
-1

试试这个,遍历每个坐标,并将每个坐标的第一个值放入名为的数组中,将每个坐标的第二个值放入名为..first的数组中。second

var input = [[45,45],[35,75],[85,90]];

function splitValues(coordinates) {
    var first = [];
    var second = [];
    for (var i = 0; i < coordinates.length; i++) {
    first.push(coordinates[i][0]);
    second.push(coordinates[i][1]);
  }
}

splitValues(input);
于 2018-11-08T18:26:47.390 回答
-1

这将有助于假设您在输入中始终有一个 2 值数组并且您只需要 2 个结果

const array = [[45,45],[35,75],[85,90]]
let first = []
let second = []
array.forEach((item)=>{
    first.push(item[0])
  second.push(item[1])
})
console.log(first)
console.log(second)
于 2018-11-08T18:27:38.540 回答