[英]Why can't I push this additional data into my array?
我正在進行API調用,這是響應
如您所見,鍵0和1的“ outcome_status”為NULL,而鍵2為填充鍵。 返回結果的數量是動態的
我希望循環結果並將“類別”和“位置”詳細信息推入數組,如果“ outcome_status”不為NULL,則希望將該數據添加到同一數組中。
問題是,在檢查result_status是否為null的IF語句中,我無法分配forloop var i;
變量作為mapData的鍵,第二次使用.push()
。
行:68未捕獲的TypeError:無法讀取未定義的屬性“ push”
var mapData = [];
function getPolApi(year,month,lat,lng,mapData) {
$.ajax({
url : "https://data.police.uk/api/crimes-at-location?date="+year+"-"+month+"&lat="+lat+"&lng="+lng,
type : "get",
async: false,
success : function(data) {
console.log(data);
mapData = [];// Empty the array of old results
for(var i = 1; i < data.length; i++) {
編輯:我想這樣做。
mapData.push([
data[i]['category'],
data[i]['location']['street']['name'],
data[i]['outcome_status']['category'],
data[i]['outcome_status']['date']
]);
但是,如果沒有outcome_status
那么它將失敗...因此,我將第二部分添加到有條件的中
if(data[i].outcome_status != null) {
//In order to access the original array by key,
// I wish to the use the forloop iteration otherwise
//mapData.push() will add the data as NEW arrays instead of adding on to the end of the existing arrays.
//Why can't I use the current index for the key here?
mapData[i].push([ //line 68
data[i]['outcome_status']['category'],
data[i]['outcome_status']['date'],
]);
}
heatmapData.push(new google.maps.LatLng(data[i].location.latitude,data[i].location.longitude));
}
console.log(mapData);
//Add results into view
for(var i = 0; i < mapData.length; i++) {
var fill = '<div class="row resu"><div class="col-xs-2 number"><h5>'+[i+1]+'</h5></div><div class="col-xs-10"><p class="text-primary">Type of crime: <span class="text-strong">'+mapData[i][0]+'</span></p><p class="text-primary">Location specifics: <span class="text-strong">'+mapData[i][1]+'</span></p></div></div>';
$('.output').append(fill);
}//endforloop
//Move the map into view of the heatmaps
moveMap(lat,lng);
//Add the heatmaps
addHeatMap();
// console.log(mapData);
},
error: function(dat) {
console.log('error');
}
});
在這里,您將i
初始化為1:
for(var i = 1; i < data.length; i++) {
然后,您將一項推送到mapData[]
:
mapData.push([
data[i]['category'],
data[i]['location']['street']['name'],
]);
幾行后,您嘗試訪問mapData[i]
...
mapData[i].push([
data[i]['outcome_status']['category'],
data[i]['outcome_status']['date'],
]);
這正在嘗試訪問mapData[1]
,但是mapData[]
只有一項。 您需要mapData[0]
。
嘗試簡單地將您的i
更改為0
而不是1
進行初始化:
for(var i = 0; i < data.length; i++) {
如果這還不夠,並且data[]
需要為1索引,那么也許mapData[i-1]
而不是mapData[i]
會適合您的需求。
一些問題:
for
循環以i等於1開始,因此即使在第一次推送之后, mapData[i]
仍然不存在,因此將push
應用於該值是沒有意義的。 push
送到現有數組,則不應將這些其他元素添加為數組的一部分,而應將它們作為單獨的參數提供給push
。 此外,當使用諸如map
類的數組方法時,代碼將變得更具功能性。
這是填充mapData
和heatmapData
的部分的樣子:
// Create a new array, with nested arrays: one per data item
mapData = data.map(function (item) {
// Use a local variable to build the sub-array:
var arr = [
item.category,
item.location.street.name,
];
if (item.outcome_status) { // add 2 more elements if they exist
arr.push( // note: no square brackets here
item.outcome_status.category,
item.outcome_status.date,
);
}
// Add it to `dataMap` by returning it to the `map` callback:
return arr;
});
// Extend the heatmap with additional locations
heatmapData = heatmapData.concat(data.map(function (item) {
return new google.maps.LatLng(item.location.latitude,item.location.longitude);
}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.