我正在尝试使用以下数据转换 JSON 字符串:
{
"locations": [{
"info": "7811 N. Octavia",
"longitude": -87.8086439,
"latitude": 42.0229656
}, {
"info": "PO Box 271743",
"longitude": -73.087749,
"latitude": 41.6032207
}, {
"info": "P.O. Box 269",
"longitude": -86.7818523,
"latitude": 34.0667074
}]
}
以下结构:
var locations = [
['Bondi Beach', -33.890542, 151.274856],
['Coogee Beach', -33.923036, 151.259052],
['Cronulla Beach', -34.028249, 151.157507],
['Manly Beach', -33.80010128657071, 151.28747820854187],
['Maroubra Beach', -33.950198, 151.259302]
];
有什么方法可以使用 JavaScript 来实现这一点吗?
最佳答案
只要顺序正确,您就可以获得这些值。
var object = { locations: [{ info: "7811 N. Octavia", longitude: -87.8086439, latitude: 42.0229656 }, { info: "PO Box 271743", longitude: -73.087749, latitude: 41.6032207 }, { info: "P.O. Box 269", longitude: -86.7818523, latitude: 34.0667074 }] },
locations = object.locations.map(Object.values);
console.log(locations);
.as-console-wrapper { max-height: 100% !important; top: 0; }
关于Javascript 将 JSON 字符串转换为数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55571439/