Javascript 将包含数组的对象插入到返回JSON中
我知道这听起来很奇怪,但原因是为了让我的JSON与 我正在调用Instagrams endpoint API,并将以下数据放入JSON中: 名称、经度、纬度 我的问题是:如何在我的原始JSON对象中创建另一个JSON对象,该对象将包含一个具有经度和纬度的数组 这是我想要的理想回报Javascript 将包含数组的对象插入到返回JSON中,javascript,json,Javascript,Json,我知道这听起来很奇怪,但原因是为了让我的JSON与 我正在调用Instagrams endpoint API,并将以下数据放入JSON中: 名称、经度、纬度 我的问题是:如何在我的原始JSON对象中创建另一个JSON对象,该对象将包含一个具有经度和纬度的数组 这是我想要的理想回报 Features: Array[size] Object: properties:Object: geometry: Object: coordinates: ar
Features: Array[size]
Object:
properties:Object:
geometry: Object:
coordinates: array[2]
这是我的尝试
complete: function(data){
var geoArray = data.map(function(item){
tempJSON = {};
geometry = new Object();
var coordinates = []
if(item.location === null){
console.log("null check");
}
else{
tempJSON.name = item.location.name;
geometry = coordinates.push(item.location.latitude,item.location.longitude);
tempJSON.geometry = geometry;
// tempJSON.geometry[1] = item.location.longitude;
}
return tempJSON;
});
return res.json({features: geoArray});
}
});
现在,当我检查chrome控制台时,它返回几何体作为geometry:2。您正在创建(并丢弃)一个名为geometry
的对象,并将其替换为一个数组(或尝试-push()
返回数组中的项目计数,而不是数组本身),然后将其分配给tempJSON
的一个成员。跳过中间变量:
var geoArray = data.map(function(item){
tempJSON = {};
if (item.location === null){
console.log("null check");
}
else {
tempJSON.name = item.location.name;
tempJSON.geometry = {
coordinates: [item.location.latitude, item.location.longitude]
};
}
return tempJSON;
});
感谢您的回复,但我需要几何体成为对象类型,它将包含一个名为坐标的数组。这可行吗?现在它使几何体成为阵列本身。是的。编辑就是为了做到这一点。