如何在angularjs中合并两个对象数组? [英] How merge two objects array in angularjs?
问题描述
我想将以下对象数组附加到 angulajs 中的现有对象数组以实现加载更多功能.
I want to append following object array with existing one in angulajs for implementing load more feature.
即,每次都将 AJAX 响应附加到现有响应中.
ie,appending AJAX response with existing one each time.
我有一个变量,$scope.actions
,它包含以下 JSON
数据,
I have one variable, $scope.actions
which contains following JSON
data,
{
"total": 13,
"per_page": 2,
"current_page": 1,
"last_page": 7,
"next_page_url": "http://invoice.local/activities/?page=2",
"prev_page_url": null,
"from": 1,
"to": 2,
"data": [
{
"id": 2108,
"action_type_id": 202,
"user_id": 1
},
{
"id": 2108,
"action_type_id": 202,
"user_id": 1
}
]
}
我想在每次使用此变量时附加以下 JSON
响应.
I want to append following JSON
response each time this variable.
{
"data": [
{
"id": 2108,
"action_type_id": 202,
"user_id": 1
},
{
"id": 2108,
"action_type_id": 202,
"user_id": 1
}
]
}
我已经尝试过 $scope.actions.data.concat(data.data);
但它不起作用并收到以下错误消息
but it is not working and getting following error message
$scope.actions.data.concat 不是函数
推荐答案
你可以使用 angular.extend(dest, src1, src2,...);
在你的情况下是:
angular.extend($scope.actions.data, data);
在此处查看文档:
https://docs.angularjs.org/api/ng/function/angular.extend一个>
否则,如果您只从服务器获取新值,则可以执行以下操作
Otherwise, if you only get new values from the server, you can do the following
for (var i=0; i<data.length; i++){
$scope.actions.data.push(data[i]);
}
这篇关于如何在angularjs中合并两个对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!