像AppData
这样映射一个对象
heatPoints = AppData.map(function (point) {
return [point.Latitude, point.Longitude];
});
返回
console.log(JSON.stringify(heatPoints));
[[49.2898,-123.1364],[49.2752,-88.150209833],[49.2286,-123.1515]]
但我需要将它们作为 JSON 字符串数组加载到 JSON Points
中,例如:
var schoolPoints = {
"Points": [
{"latitude":49.2898,"longitude":-123.1364},
{"latitude":49.2752,"longitude":-123.0719},
{"latitude":49.2286,"longitude":-123.1515}
]
};
你能告诉我怎么做吗?
最佳答案
更改您的 map 以返回对象而不是数组,如下所示:
heatPoints = AppData.map(function (point) {
return {
"latitude": point.Latitude,
"longitude": point.Longitude
};
});
然后您可以另外将其设置为变量的属性,以获取您请求的特定 JSON:
heatPoints = {"Points": heatPoints};
这将生成您请求的确切 JSON。
关于javascript - 数组到 JSON 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41131273/