【发布时间】:2019-11-25 17:41:34
【问题描述】:
所以我有一个 API 将 JSON 输出到我的 JS 代码 (http://api.opentripmap.com/0.1/ru/places/bbox?lon_min=-123.049641&lat_min=37.550392&lon_max=-122.049641&lat_max=38.550392&kinds=foods&format=geojson&apikey=5ae2e3f221c38a28845f05b685eac8210f10fb196793a9d4f6653c25)。
但是它包含一个看起来像这样的 JSON 数组-"coordinates": [ -122.510216, 37.769474 ]
有没有办法将其拆分为单独的 JS 变量,例如左侧一个变量,右侧另一个变量。目前该数组导致我的代码崩溃,因为它在每个插槽中只能接受一个输入。
对不起,如果这是一个简单的问题,我还没能解决这个问题......
编辑: 对不起,糟糕的问题布局。我尝试拼接和拆分数组但没有成功(拼接会导致一大堆未定义的错误)。
我当前的代码是
module.exports.function = function findLunch(myLocation) {
var loc_long_max = Number(myLocation.longitude) //grab longitude from user
var loc_lat_min = Number(myLocation.latitude) //grab latitude from User
var loc_long_min = loc_long_max - 0.5;
var loc_lat_max = loc_lat_min + 0.5;
var url_all = "http://api.opentripmap.com/0.1/ru/places/bbox?lon_min=" + loc_long_min + "&lat_min=" + loc_lat_min + "&lon_max=" + loc_long_max + "&lat_max=" + loc_lat_max + "&kinds=foods&format=geojson&apikey=5ae2e3f221c38a28845f05b685eac8210f10fb196793a9d4f6653c25"
var results = http.getUrl(url_all, { format: 'json' }); //gets json info from url_all.
console.log(results.features[rand].properties.name)
//console.log(results.feautres[rand].properties.rate)
console.log(results.features[rand].geometry.coordinates)
//console.log(results);
for (var i = rand; i < results.features.length; i++) {
console.log(results.features[rand].properties.name)
console.log(results.features[rand].properties.rate)
var businesses = {
name: results.features[rand].properties.name,
rating:results.features[rand].properties.rate,
coordinates: results.features[rand].geometry.coordinates
}
}
return businesses
所以坐标需要被拆分,然后在businesss var中,然后输出到Bixby....
编辑 2:已修复 - 感谢大家的帮助!
谢谢!
【问题讨论】:
-
您的问题表明您没有尝试自己解决问题,您的代码也未包含在问题中,因此您可能会收到一些负面反馈。请花一些时间熟悉how to ask 并编辑您的问题以使其处于更好的状态。关于您的问题,请查看array destructuring assignments。
标签: javascript arrays json bixby