我有一个问题,我想将json输出编辑为这种格式:
[ 50.87758, 5.78092 ], [ 52.87758, 5.48091 ]
等。
目前我有这个输出:
[ { lat: 53.1799, lon: 6.98565 },
{ lat: 52.02554, lon: 5.82181 },
{ lat: 51.87335, lon: 4.34166 },
{ lat: 50.87758, lon: 5.78092 } ]
我已经尝试过str.replace,但无法让它发挥作用。 这是我目前的代码:
> 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); })
任何帮助表示赞赏!!
答案 0 :(得分:0)
将Array#map
与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);
或者你可以推送array
代替Object
密钥lat
和lon
radars.push([result.roadEntries[entry].events.radars[0].loc.lat,result.roadEntries[entry].events.radars[0].loc.lon])