在现有json对象中添加新键(数组)

Kor*_*rte 0 javascript json push http angularjs

我有一个带对象的数组.我需要为每个对象添加一个键,它将是一个其他对象的数组.

所以我的代码看起来像这样:

$scope.array = [];

$http.get(url).success(function(data) {

   $scope.array = data;

   // Now my array has some objects
   var i = 0;
   function() getSomeData(i) { 
      if(i<array[i].length()) {
         $http.get(url + array[i].someKey).success(function(data){
            $scope.array[i].push(data);
            i++;
            getSomeData(i);
         })
      } 
   }

})

getSomeData(0);
Run Code Online (Sandbox Code Playgroud)

但我得到了 Error:array.push is not a function

为什么会这样?

小智 5

您正在尝试推送到对象而不是数组.要么:

$scope.array.push(data) 添加新对象.

或者 $scope.array[i] = data更新数组中特定索引处的对象.