[
{ID: "123", lat: "55.680024", lng: "37.558505"},
{ID: "123", lat: "55.680024", lng: "37.558505"},
{ID: "987", lat: "55.783861", lng: "37.536533"},
{ID: "987", lat: "55.783861", lng: "37.536533"}
]
[
{
ID: "123",
Coords: [
{lat: "55.680024", lng: "37.558505"},
{lat: "55.680024", lng: "37.558505"}
]
},
{
ID: "987",
Coords: [
{lat: "55.783861", lng: "37.536533"},
{lat: "55.783861", lng: "37.536533"}
]
}
]
var arr = [
{ID: "123", lat: "55.680024", lng: "37.558505"},
{ID: "123", lat: "55.680024", lng: "37.558505"},
{ID: "987", lat: "55.783861", lng: "37.536533"},
{ID: "987", lat: "55.783861", lng: "37.536533"}
];
console.log( convert(arr));
function convert(arr){
var newarr = [];
arr.map((e)=>{
for(var i = 0;i < newarr.length;i++)
if(newarr[i].ID == e.ID){
newarr[i].Coords.push({lat:e.lat,lng:e.lng});
return;
}
newarr.push({ID:e.ID,Coords:[{lat:e.lat,lng:e.lng}]});
});
return newarr;
}
const map = {};
srcArray.forEach(({ ID, ...Coords }) => {
map[ID] = map[ID] || { ID, Coords: [] };
map[ID].Coords.push(Coords);
});
const resultArray = Object.values(map);
const map = {};
srcArray.forEach(({ ID, lat, lng }) => {
map[ID] = map[ID] || { ID, Coords: [] };
map[ID].Coords.push({ lat, lng });
});
const resultArray = Object.values(map);
const resultArray =
Object.values(srcArray.reduce((map, { ID, ...Coords }) => {
map[ID] = map[ID] || { ID, Coords: [] };
map[ID].Coords.push(Coords);
return map;
}, {}));
const resultArray =
Object.values(srcArray.reduce((map, { ID, lat, lng }) => {
map[ID] = map[ID] || { ID, Coords: [] };
map[ID].Coords.push({ lat, lng });
return map;
}, {}));
var map = {};
var resultArray = [];
srcArray.forEach(function(el) {
map[e.ID] = map[el.ID] || { ID: el.ID, Coords: [] };
map[el.ID].Coords.push({ lat: el.lat, lng: el.lng });
});
for(var key in map) {
resultArray.push(map[key]);
}