0

I have one array like below,

                    $scope.unassociatedResults = [{
                                matchName : 'Dummy_data',
                                address : 'Dummy_data',
                                country : 'Dummy_data',
                                listSourceCountryName : 'Dummy_data',
                                screeningCategory : 'Dummy_data'
                    },
                    {
                                matchName : 'Dummy_data2',
                                address : 'Dummy_data2',
                                country : 'Dummy_data2',
                                listSourceCountryName : 'Dummy_data2',
                                screeningCategory : 'Dummy_data2'
                    },
                   {
                                matchName : 'Dummy_data3',
                                address : 'Dummy_data3',
                                country : 'Dummy_data3',
                                listSourceCountryName : 'Dummy_data3',
                                screeningCategory : 'Dummy_data3'
                    },
                    {
                                matchName : 'Dummy_data4',
                                address : 'Dummy_data4',
                                country : 'Dummy_data4',
                                listSourceCountryName : 'Dummy_data4',
                                screeningCategory : 'Dummy_data4'
                    },
            ];

Now, I have other array which has parameters like below

           $scope.unassociatedResultsNewArray = [{
                            matchName : 'Dummy_data',
                            address : 'Dummy_data',
                            country : 'Dummy_data',
                            listSourceCountryName : 'Dummy_data',
                            screeningCategory : 'Dummy_data'
                }];

how can I check atleast two, three values of new array to existing array list or how can I compare whole new array with existing array list, to avoid the duplication. In my case I have no id or no primary something like to check or to compare data. So I need to check whole array with existing array list.

How can I check it in angularjs or in javascript ?

3
  • You can try to stringify it and compare both objects. Commented Mar 23, 2018 at 11:25
  • 1
    Possible duplicate of Array.push() if does not exist? Commented Mar 23, 2018 at 11:28
  • Stringify objects and compare it JSON.stringify({name: "ss"}) === JSON.stringify({name: "ss"}) Commented Mar 23, 2018 at 11:31

1 Answer 1

1

Try this.

for (var i = 0; i < array1.length; ++i) {
    for (var j = 0; j < array2.length; j++) {
        if (JSON.stringify(array2[j]) === JSON.stringify(array1[i]))
            array2.splice(j, 1);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I guess you meant JSON.stringify(array2[j] )=== JSON.stringify(array1[i])). Will stringify ensure the order of object's attribute, since it isn't array?
Yes, I wanted same, except slice , i want to push arry2 in array1 in its next index. I have tried below code, but it is not working for (var i = 0; i < $scope.unassociatedResults.length; ++i) { for (var j = 0; j < $scope.unassociatedResultsNewArray.length; j++) { if (JSON.stringify($scope.unassociatedResultsNewArray[j]) === JSON.stringify($scope.unassociatedResults[i])){ console.log("hello"); $scope.unassociatedResultsNewArray.splice(j, 1); } else { $scope.unassociatedResults.push(j, 1); } } }

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.