-1

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!!

  • `input.map(el => [el.lat, el.lon])` – Rayon Sep 28 '16 at 11:51
  • If I am wrong `radars.push([result.roadEntries[entry].events.radars[0].loc.lat,result.roadEntries[entry].events.radars[0].loc.lon])` – Rayon Sep 28 '16 at 11:54
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Michał Perłakowski Sep 28 '16 at 11:54

1 Answers1

0

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])
Rayon
  • 36,219
  • 4
  • 49
  • 76
  • `Array.prototype.map()` was introduced in ES 5.1. – Michał Perłakowski Sep 28 '16 at 11:53
  • `\anwb.js:11 radars.push([result.roadEntries[entry].events.radars[0].loc.??lat,result .roadEntri??es[entry].events.rad??ars[0].loc.lon]; ^ SyntaxError: Unexpected token ILLEGAL` – Michael Smits Sep 28 '16 at 12:03
  • Thank you very much! Was ANSI issue! Output is great, thanks a lot! – Michael Smits Sep 28 '16 at 12:38
  • @Rayon Do you know a trick to remove the first space and the last space? Now its: `[ 52.02575, 5.82325 ]` but it needs to be `[52.02575, 5.82325]` Thank you! – Michael Smits Sep 28 '16 at 12:58
  • @MichaelSmits — Use `radars.push([result.roadEntries[entry].events.radars[0].loc.‌​lat.trim(),result.roadEntri‌​es[entry].events.rad‌​ars[0].loc.lon].trim()) ` – Rayon Sep 28 '16 at 13:03
  • @Downvoter, I failed to understand the reason for _downvote_, your _comment_ is precious for me! – Rayon Sep 28 '16 at 13:23
  • @Rayon Thank you very much for your help, i really appreciate that! I'm very new to javascript, and got the following error... `Error calling api [TypeError: result.roadEntries[entry].events.radars[0].loc.lat .trim is not a function]` .. can you please help me out for the last time? Hehe! – Michael Smits Sep 28 '16 at 13:33