[英]Add a new key (array) in an existing json object
I've got an array with objects. 我有一个对象数组。 I need for each object to add a key, which will be an array of other objects.
我需要为每个对象添加一个键,该键将是其他对象的数组。
So my code looks like this: 所以我的代码看起来像这样:
$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);
But I am getting Error:array.push is not a function
但我收到
Error:array.push is not a function
Why's that happening ? 为什么会这样呢?
You are trying to push to an object instead of the array. 您正在尝试推送到对象而不是数组。 Either do:
要么做:
$scope.array.push(data)
to add a new object. $scope.array.push(data)
添加一个新对象。
or do $scope.array[i] = data
to update the object at the specific index in the array. 或执行
$scope.array[i] = data
以更新数组中特定索引处的对象。
Objects in JSON are added using below two techniques. 使用以下两种技术添加JSON中的对象。 You can try one of them.
您可以尝试其中之一。
object["property"] = value;
or 要么
object.property = value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.