0

I have the following 2 arrays $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
}];

Here I need to compare these 2 arrays, then delete the changed object in $scope.oldArray & add the changed object from $scope.newArray to $scope.oldArray.

Note: Should not replace all the values from $scope.newArray to $scope.oldArray.

1 Answer 1

1

objects of arrays to compare can use 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;
                }
            });
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.