My current array of objects looks like,
const data = [
{
"Type": "Location",
"Name": "Water",
},
{
"Type": "Location",
"Coordinates": [
{
"Lat": 57.94182777439993,
"Long": 79.50404114815193
},
{
"Lat": 31.209277877460135,
"Long": 78.80122177677728
},
{
"Lat": 31.35950051982242,
"Long": 105.15694820332524
},
{
"Lat": 58.17432360099434,
"Long": 105.42050546759074
}
],
"Name": "Water",
},
{
"Type": "Location",
"Coordinates": [
{
"Lat": 58.72972797827911,
"Long": 76.90570777266291
},
{
"Lat": 29.54717721331581,
"Long": 76.37859324413196
},
{
"Lat": 30.460511875130663,
"Long": 105.19418747049103
},
{
"Lat": 59.71902258556691,
"Long": 106.7755310560839
}
],
"Name": "Water",
}
];
Which need to be converted into this format,
[
{
"name": "Water",
"coords": [
[57.94182777439993, 79.50404114815193],
[31.209277877460135, 78.80122177677728],
[31.35950051982242, 105.15694820332524],
[58.17432360099434, 105.42050546759074]
]
},
{
"name": "Water",
"coords": [
[58.72972797827911, 76.90570777266291],
[29.54717721331581, 76.37859324413196],
[30.460511875130663, 105.19418747049103],
[59.71902258556691, 106.7755310560839]
]
}
]
I tried,
const output = data.reduce((accumulator, curr) => {
if(curr.Coordinates) {
const data = curr.Coordinates.map(({Lat, Long}) => [Lat, Long]);
accumulator.push(data)
};
return accumulator;
}, []);
But, the code above stored my coords to a array of list.
nameproperty in the last entry.