[
{"type":"1","text":"success"},
{"yid":"167","uid":"15","lat":"13.0501383","lng":"77.597593","time":"2014-10-20 20:25:27","dist":1.95},
{"yid":"204","uid":"32","lat":"13.0412773","lng":"77.6133699","time":"2014-11-17 03:15:24","dist":0.06}
]这是来自php脚本的json响应。我需要在javascript中将其存储或推送到多维数组中。我可以使用下面的代码成功地解码json响应。
var myLatLng = [];
var myYid = [];
post_data = {'userLat':position.coords.latitude, 'userLng':position.coords.longitude};
$.post('leo.php', post_data, function(response){
$.each(response, function(index, element) {
if(element.lat!=undefined || element.lng!=undefined)
{
myLatLng.push(element.lat+', '+element.lng);
myYid.push(element.yid);
}
});
});我还需要在不同的函数中全局使用这个值。
发布于 2015-01-22 21:07:00
首先,您希望对undefined的检查是一个" and“条件,因为必须定义这两个条件,因为您正在访问这两个条件,并在"then”子句中将这两个条件赋给一个变量。
当你说“多维数组”时,你可以在javascript中拥有一个多维数组,如果它只有数字索引的话。如果您想要键值对,那么它将是一个对象,而不是数组。如果这对你来说已经足够好了,那么每次只需推入一个数组,而不是将单个值推入两个单独的数组:
var multiDimArray = [];
post_data = {'userLat':position.coords.latitude, 'userLng':position.coords.longitude};
$.post('leo.php', post_data, function(response){
$.each(response, function(index, element) {
if(element.lat!==undefined && element.lng!==undefined)
{
multiDimArray.push(new Array(element.lat+', '+element.lng,element.yid));
}
});
});https://stackoverflow.com/questions/28087754
复制相似问题