Hi there,
Question: I am looking to convert the following data structure:
console.log(convertCoordinates([
{ "name": "Salvador", "coordinates": ["-23.6821604","-46.8754915"]},
{ "name": "Brasília", "coordinates": ["-22.9137531","-73.5860657"]},
{ "name": "Recife", "coordinates": [ "-8.0462741","-35.0000824"]}
]));
into this format?
{
'0':
{ name: 'Salvador',
coordinates: [-46.8755, -23.6822 ] },
'1':
{ name: 'Brasília',
coordinates: [-73.5861, -22.9137] },
'2': { name: 'Recife',
coordinates: [-35.0001, -8.0463] }
}
Differences between the two results.
First Format:
- is an array of objects
- Contains city names and coordinates as string arrays.
Second Format:
- is a single object
- Contains city names (same as in the first format) and coordinates as decimal numbers with 4 digits after the dot;
- The positions of longitude and latitude are swapped - longitude comes first.
Language: JavaScript
I attempted to use the ** Object.assign ** method but couldn't achieve the desired changes with the "coordinates".
function convertCoordinates(array){
let obj = Object.assign({},array);
return obj;
}
// Test
console.log(convertCoordinates([
{ "name": "Salvador", "coordinates": ["-23.6821604","-46.8754915"]},
{ "name": "Brasília", "coordinates": ["-22.9137531","-73.5860657"]},
{ "name": "Recife", "coordinates": [ "-8.0462741","-35.0000824"]}
]));
Can someone provide assistance with this function puzzle?