I have an array of nested arrays containing coordinates. I would like to create a new array containing nested arrays of coordinates based on if they have the same latitude. The description might be a little confusing, so here some examples and code
Initial array (latitude is the second value of the number pairs)
const coordinateArray = [[46,11], [38,11], [44,9], [81,15], [55,15]];
Expected outcome:
const newArray = [
[[46,11],[38,11]],
[[81,15],[55,15]],
[[44,9]]
];
I tried this, but it returns every coordinate in a new array instead of pairing the ones with same latitude:
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);
});
Would be grateful for any suggestions