Issue
{
"resource": [{
"devid": "862057040386432",
" time": "2021-11-02 12:36:30",
"etype": "G_PING",
"engine": "OFF",
"lat": "9.235940",
"lon": "78.760240",
"vbat": "3944",
"speed":"7.49",
"plnt": "10",
"fuel": 0
}]
}
I have access to the "resource" JSON Array at this point, but am unsure as to how I'd get the "lat" and "lon" values within a for loop. Sorry if this description isn't too clear, I'm a bit new to programming.
Solution
const data = {
resource: [{
devid: "862057040386432",
time: "2021-11-02 12:36:30",
etype: "G_PING",
engine: "OFF",
lat: "9.235940",
lon: "78.760240",
vbat: "3944",
speed:"7.49",
plnt: "10",
fuel: 0
}
]
};
const latAndLongVersionOne = data.resource.map(res => [res.lat, res.lon]);
const latAndLongVersionTwo = data.resource.map(({lat, lon}) => [lat, lon]);
const latAndLongVersionThree = data.resource.map(({lat, lon}) => ({lat, lon}));
console.log('Lat and long: ', latAndLongVersionOne); // Nested array with long and lat (values names has to be inferred)
console.log('Lat and long: ', latAndLongVersionTwo); // Same result with another syntax
console.log('Lat and long: ', latAndLongVersionThree); // Array of objects with caption
Answered By - Michael Bahl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.