如何在angularjs中合并两个对象数组?

时间:2022-01-29 12:13:20

I want to append following object array with existing one in angulajs for implementing load more feature.

我想在angulajs中添加跟随对象数组以实现load more特性。

ie,appending AJAX response with existing one each time.

ie,每次都添加一个AJAX响应。

I have one variable, $scope.actions which contains following JSON data,

我有一个变量,$scope。包含以下JSON数据的操作,

    {
    "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
        }
    ]
}

I want to append following JSON response each time this variable.

每次这个变量时,我都希望在JSON响应之后追加。

    {
    "data": [
        {
            "id": 2108,
            "action_type_id": 202,
            "user_id": 1
        },
        {
            "id": 2108,
            "action_type_id": 202,
            "user_id": 1
        }
    ]
}

I have tried with $scope.actions.data.concat(data.data);

我尝试过$scope.action .data.concat(data.data);

but it is not working and getting following error message

但是它不能正常工作并得到以下错误消息

$scope.actions.data.concat is not a function

scope.actions.data美元。concat不是函数

4 个解决方案

#1


37  

You can use angular.extend(dest, src1, src2,...);

您可以使用角。扩展(桌子,src1,src2,…);

In your case it would be :

就你而言,它将是:

angular.extend($scope.actions.data, data);

See documentation here :

看到文档:

https://docs.angularjs.org/api/ng/function/angular.extend

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]);
}

#2


21  

This works for me :

这对我很有效:

$scope.array1 = $scope.array1.concat(array2)

In your case it would be :

就你而言,它将是:

$scope.actions.data = $scope.actions.data.concat(data)

#3


4  

$scope.actions.data.concat is not a function 

same problem with me but i solve the problem by

我也有同样的问题,但是我通过

 $scope.actions.data = [].concat($scope.actions.data , data)

#4


2  

Simple

简单的

var a=[{a:4}], b=[{b:5}]

angular.merge(a,b) // [{a:4, b:5}]

Tested on angular 1.4.1

测试角1.4.1

#1


37  

You can use angular.extend(dest, src1, src2,...);

您可以使用角。扩展(桌子,src1,src2,…);

In your case it would be :

就你而言,它将是:

angular.extend($scope.actions.data, data);

See documentation here :

看到文档:

https://docs.angularjs.org/api/ng/function/angular.extend

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]);
}

#2


21  

This works for me :

这对我很有效:

$scope.array1 = $scope.array1.concat(array2)

In your case it would be :

就你而言,它将是:

$scope.actions.data = $scope.actions.data.concat(data)

#3


4  

$scope.actions.data.concat is not a function 

same problem with me but i solve the problem by

我也有同样的问题,但是我通过

 $scope.actions.data = [].concat($scope.actions.data , data)

#4


2  

Simple

简单的

var a=[{a:4}], b=[{b:5}]

angular.merge(a,b) // [{a:4, b:5}]

Tested on angular 1.4.1

测试角1.4.1