Reputation: 3409
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.
Upvotes: 0
Views: 68
Reputation: 19986
Just update your reduce a little bit.
Rather than pushing the coordinates array directly to the accumulator
, make it as an object
with name
and coords
as keys. Push this object to the accumulator
to get your desired output.
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",}];
const output = data.reduce((accumulator, curr) => {
if (curr.Coordinates) {
const data = {
name: curr.Name,
coords: curr.Coordinates.map(({ Lat, Long }) => [Lat, Long]),
}
accumulator.push(data)
};
return accumulator;
}, []);
console.log(output);
Upvotes: 1
Reputation: 350272
In the reduce callback, just wrap the mapped coordinates in an object literal that also includes the name property:
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",}];
const output = data.reduce((accumulator, curr) => {
if(curr.Coordinates) {
const Coordinates = curr.Coordinates.map(({Lat, Long}) => [Lat, Long]);
accumulator.push({ Name: curr.Name, Coordinates});
};
return accumulator;
}, []);
console.log(output);
Upvotes: 2