AngularJS - 比较&替换2个数组之间的对象

时间:2015-10-14 08:14:28

标签: javascript arrays angularjs angularjs-scope

我有以下2个阵列SELECT ques_id, question, ans FROM tbl_questions t1 where not exists (select 1 from tbl_questions t2 where t2.ques_id = t1.ques_id and t2.id > t1.id) & $scope.oldArray

$scope.newArray

这里我需要比较这两个数组,然后在$scope.oldArray = [{ "status": "New", "priority_summary": "High", "u_id" : 1 }, { "status": "New", "priority_summary": "High", "u_id" : 2 }, { "status": "New", "priority_summary": "High", "u_id" : 3 }, { "status": "New", "priority_summary": "High", "u_id" : 4 }]; $scope.newArray = [{ "status": "Old", "priority_summary": "Low", "u_id" : 1 }, { "status": "Old", "priority_summary": "High", "u_id" : 2 }, { "status": "New", "priority_summary": "Low", "u_id" : 3 }, { "status": "New", "priority_summary": "High", "u_id" : 4 }]; &中删除已更改的对象。将更改后的对象从$scope.oldArray添加到$scope.newArray

注意:不应将$scope.oldArray中的所有值替换为$scope.newArray

1 个答案:

答案 0 :(得分:1)

要比较的数组对象可以使用angular.forEach

     angular.forEach(oldArray , function(value1, key1) {
            angular.forEach(newArray , function(value2, key2) {
                if (!(value1.status == value2.status&& 
                    value1.priority_summary==value2.priority_summary 
                    && value1.u_id==value2.u_id)) {
                    value1.status=value2.status;
                    value1.priority_summary=value2.priority_summary;
                    value1.u_id=value2.u_id;
                }
            });
    });