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 ?

1 Answers1

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);
    }
}
Ininiv
  • 1,325
  • 7
  • 12
  • 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? – Satish Kumar Mar 23 '18 at 20:38
  • 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); } } } – angular newpiee Mar 24 '18 at 07:17