如何在angularjs中合并两个对象数组? [英] How merge two objects array in angularjs?

查看:596
本文介绍了如何在angularjs中合并两个对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在angulajs中添加以下对象数组和现有对象数组,以实现更多加载功能.

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

即,每次将AJAX响应附加到现有的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 is not a function

推荐答案

您可以使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆