I have an array of object like this
{
"data": [{
"location": {
"type": "Point",
"coordinates": [
77.361916,
28.606075
]
},
}]
}
I want to get the latitude and longitude from this and mutate the object to something like this:
{
"data": [{
"latitude": 28.606075,
"longitude": 77.361916,
}]
}
How can i achieve this? I tried by doing
response.map((data: any) => {
console.log(data.location);
let latitude = data.location.coordinates[2];
let longitude = data.location.coordinates[1];
delete data.location;
data.latitude = latitude;
data.longitude = longitude;
})
I know this is wrong but how can i fix this?
If you only have one set
const oldObj = { "data": [{ "location": { "type": "Point", "coordinates": [ 77.361916, 28.606075 ] }, }] } const [longitude, latitude] = [...oldObj.data[0].location.coordinates] const newObj = { data: [{ latitude, longitude }] } console.log(newObj)
For more
const oldArr = [{ "data": [{ "location": { "type": "Point", "coordinates": [ 77.361916, 28.606075 ] }, }] }, { "data": [{ "location": { "type": "Point", "coordinates": [ 77.361916, 28.606075 ] }, }] }, { "data": [{ "location": { "type": "Point", "coordinates": [ 77.361916, 28.606075 ] }, }] } ] const newArr = oldArr.map(item => { const [longitude, latitude] = [...item.data[0].location.coordinates]; return { data: [{ latitude, longitude }] } }) console.log(newArr)
response.map((data: any) => {
let newData = {"data" : []};
let coords = {
'latitude' : data.location.coordinates[2],
'longitude' : data.location.coordinates[1]
}
newData.data.push(coords)
return newData;
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.