Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么可以';我不能将这些附加数据推送到阵列中吗?_Javascript_Jquery_Arrays - Fatal编程技术网

Javascript 为什么可以';我不能将这些附加数据推送到阵列中吗?

Javascript 为什么可以';我不能将这些附加数据推送到阵列中吗?,javascript,jquery,arrays,Javascript,Jquery,Arrays,我正在进行一个API调用,下面是响应 如您所见,键0和1的“结果_状态”为NULL,而键2已填充返回结果的数量是动态的 我希望循环结果并将“category”和“location”详细信息推送到数组中,如果“output_status”不为NULL,那么我希望将该数据添加到同一数组中 问题是,在检查结果状态是否为null的IF语句中,我无法分配forloopvar I变量作为 第二次使用.push()时使用的mapData 第68行未捕获类型错误:无法读取未定义的属性“push” 但如果没有结

我正在进行一个API调用,下面是响应

如您所见,键0和1的“结果_状态”为NULL,而键2已填充返回结果的数量是动态的

我希望循环结果并将“category”和“location”详细信息推送到数组中,如果“output_status”不为NULL,那么我希望将该数据添加到同一数组中

问题是,在检查结果状态是否为null的IF语句中,我无法分配forloop
var I变量作为
第二次使用
.push()
时使用的mapData

第68行未捕获类型错误:无法读取未定义的属性“push”

但如果没有
结果\u状态
则它将失败。。。因此,我将第二部分添加到条件中

         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');
    }
 });
if(数据[i]。结果状态!=null){
//要通过密钥访问原始阵列,
//否则,我希望使用forloop迭代
//push()将数据作为新数组添加,而不是添加到现有数组的末尾。
//为什么我不能在这里使用当前索引作为键?
mapData[i].推送([//第68行)
数据[i][“结果状态”][“类别”],
数据[i][“结果状态”][“日期”],
]);
}
heatmapData.push(新的google.maps.LatLng(数据[i]。位置。纬度,数据[i]。位置。经度));
}
console.log(mapData);
//将结果添加到视图中
对于(var i=0;i犯罪类型:'+mapData[i][0]+'

位置详细信息:'+mapData[i][1]+'

; $('.output')。追加(填充); }//endforloop //将地图移动到热图视图中 移动地图(lat、lng); //添加热图 addHeatMap(); //console.log(mapData); }, 错误:函数(dat){ console.log('error'); } });
问题: 这里,您将
i
初始化为1:

for(var i = 1; i < data.length; i++) {

几行之后,您尝试访问
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++) {
for(变量i=0;i
如果这还不够,并且
data[]
需要作为1-index,那么可能
mapData[i-1]
而不是
mapData[i]
会满足您的需要。

问题: 这里,您将
i
初始化为1:

for(var i = 1; i < data.length; i++) {

几行之后,您尝试访问
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++) {
for(变量i=0;i
如果这还不够,并且
data[]
需要作为1-index,那么也许
mapData[i-1]
而不是
mapData[i]
可以满足您的需要。

一些问题:

  • 您的
    for
    循环以i等于1开始,因此即使在第一次推送之后,
    mapData[i]
    仍然不存在,因此对其应用
    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);
}));
一些问题:

  • 您的
    for
    循环以i等于1开始,因此即使在第一次推送之后,
    mapData[i]
    仍然不存在,因此对其应用
    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);
}));

mapData[i]
是有问题的对象。你想用推送来完成什么?如果(!data[i].output\u status==null)
这个
if(data[i].output\u status!==null)
i
初始化为
1
,并且你调用
mapData.push()
一次,然后在第一次迭代中,
mapData[]
当它到达
mapData[i]时只有一项。push(…)
。当
mapData[]
只有一项时,它试图调用
mapData[1]
上的
push()在forloop内部…我正在尝试使用
I
以便将数据添加回我在上面设置的相同数组。@proeviz但是
for
循环从1开始,而不是从0开始。
mapData[I]
是有问题的对象。您试图通过推送实现什么?如果(!data[I]。output\u status==null),是否应该使用
如果您的
for
i
初始化为
1
,并且您调用
mapData.push()
一次,然后在第一次迭代中,
mapData[]
到达
mapData[i]时只有一项。push(…)
。它试图调用