Michael Smits Michael Smits - 2 months ago 15
JSON Question

Format coordinates format javascript [NODEJS]

I have a question, i want to edit the json output to this format:

[ 50.87758, 5.78092 ], [ 52.87758, 5.48091 ]
etc.

Currently i have this output:

[ { lat: 53.1799, lon: 6.98565 },
{ lat: 52.02554, lon: 5.82181 },
{ lat: 51.87335, lon: 4.34166 },
{ lat: 50.87758, lon: 5.78092 } ]


I've already tried it with str.replace, but can't get it to work.
This is my current code:

> var http = require('http.min')
>
> function getRadars (callback) { var radars = [] var options = {
> uri: 'http://www.anwb.nl/feeds/gethf' } http.json(options).then(function (result) {
> Object.keys(result.roadEntries).forEach(function (entry) {
> if (result.roadEntries[entry].events.radars.length > 0) {
> radars.push(result.roadEntries[entry].events.radars[0].loc)
>
> }
> })
> callback(null, radars) }).catch(function (reason) {
> console.log('Error calling api', reason)
> callback(reason) }) }
>
> getRadars(function (reason, radars) {
>
> console.log(radars); })


Any help is appreciated!!

Answer

Using Array#map with arrow-function[ES6]

var inp = [{
  lat: 53.1799,
  lon: 6.98565
}, {
  lat: 52.02554,
  lon: 5.82181
}, {
  lat: 51.87335,
  lon: 4.34166
}, {
  lat: 50.87758,
  lon: 5.78092
}];
var op = inp.map(el => [el.lat, el.lon]);
console.log(op);

Or you can just push array instead of Object having keys as lat and lon

radars.push([result.roadEntries[entry].events.radars[0].loc.‌​lat,result.roadEntri‌​es[entry].events.rad‌​ars[0].loc.lon])
Comments