I have a function that makes an HTTP GET request to compare a given value with elements in an array. If there's no match, I want to add the value to another array. The issue is that duplicates are being added to the array because of multiple loop iterations. Here is my code:
$scope.fetchParcel = function()
{
$http({
method: 'GET',
url: '/infiniti/' + $scope.infinitiId,
})
.success(function(a){
angular.forEach($scope.parcels, function(parcel, key){
angular.forEach(parcel, function(par){
if(par == $scope.shipper_ref_no){
$scope.scans.push(parcel);
$scope.checked++;
$scope.parcels.splice(key, 1);
}
if(par != $scope.shipper_ref_no){
$scope.excludeds.push($scope.shipper_ref_no);
}
});
});
});
}
The part where it says
$scope.excludes.push($scope.shipper_ref_no);
correctly adds the shipper_ref_no to the array, but due to multiple elements in the array, it gets added multiple times when it should only be added once after confirming that no matches were found. How can I achieve this?
Example of the array:
[
-{
id: 1
t: "1122"
srn: "23"
}
-{
id: 2
t: "234"
srn: "23"
}
-{
id: 3
t: "dd"
srn: "44"
}
]
If my input is something like "444kkkddd" and it doesn't match any element, it adds "444kkkddd" to the array multiple times. I need it to be added just once. I understand why it happens, but I'm seeking a solution to handle this so it adds just one entry. I'm stuck.