将 JSON 数组拆分为 JS 变量 - Bixby
Split a JSON array into JS variables - Bixby
所以我有一个 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
所以坐标需要拆分,然后放在业务变量中,然后输出到 Bixby....
编辑 2:已修复 - 感谢大家的帮助!
谢谢!
您可以使用destructuring将数组的第一个和第二个元素简单地赋值给变量:
const coordinates = [-122.510216, 37.769474];
const [left, right] = coordinates;
console.log(left);
console.log(right);
不确定这是否是您要问的问题,但您可以使用 destructuting assignment 将数组中的项目分配给变量:
const coordinates = [ -122.510216, 37.769474 ];
const [coordinateOne, coordinateTwo] = coordinates;
console.log(coordinateOne) // -122.510216
console.log(coordinateTwo) // 37.769474
所以我有一个 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
所以坐标需要拆分,然后放在业务变量中,然后输出到 Bixby....
编辑 2:已修复 - 感谢大家的帮助!
谢谢!
您可以使用destructuring将数组的第一个和第二个元素简单地赋值给变量:
const coordinates = [-122.510216, 37.769474];
const [left, right] = coordinates;
console.log(left);
console.log(right);
不确定这是否是您要问的问题,但您可以使用 destructuting assignment 将数组中的项目分配给变量:
const coordinates = [ -122.510216, 37.769474 ];
const [coordinateOne, coordinateTwo] = coordinates;
console.log(coordinateOne) // -122.510216
console.log(coordinateTwo) // 37.769474