我有一个包含坐标的嵌套数组数组。我想创建一个包含嵌套坐标数组的新数组,基于它们是否具有相同的纬度。描述可能有点混乱,所以这里有一些示例和代码
初始数组(纬度是数字对的第二个值)
const coordinateArray = [[46,11], [38,11], [44,9], [81,15], [55,15]];
预期结果:
const newArray = [
[[46,11],[38,11]],
[[81,15],[55,15]],
[[44,9]]
];
我试过这个,但它返回一个新数组中的每个坐标,而不是配对具有相同纬度的坐标:
const rowArrays = [];
coordinateArray.map(c => {
const row = [c];
for (let i = 0; i < coordinateArray.length; i++) {
console.log(c[1], coordinateArray[i][1]);
if (c[1] === [1]) {
row.push(coordinateArray[i]);
coordinateArray.splice(0, 1);
}
}
return rowArrays.push(row);
});
将不胜感激任何建议