angular newpiee
angular newpiee

Reputation: 41

Prevent pushing array in other array angular js

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 ?

Upvotes: 0

Views: 70

Answers (1)

Ininiv
Ininiv

Reputation: 1325

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

Upvotes: 1

Related Questions