I am working on a task where I need to ensure that the object being added to an array of objects is not already present in a temporary array of objects.
While using AngularJS, I encountered an error stating "Duplicates in a repeater are not allowed." This error cannot be caught using try/catch blocks.
Is there a way to check for this specific exception or to verify if the object already exists in the array of objects?
Any assistance on this matter would be greatly appreciated.
CODE EXAMPLE:
$scope.availableSounds = [
{
name: "Rain"
},
{
name: "Storm"
},
{
name: "Forest"
},
];
$scope.selectedSounds = [];
ADDING OF ITEMS (OBJECT IN THE ARRAY OF THE SELECTED SOUND SHOULD BE UNIQUE)
$scope.addSoundToSelection = function(index) {
try {
var selectedItem = $scope.availableSounds[index];
$scope.selectedSounds.push(selectedItem);
var pos = $scope.selectedSounds.map(function(e) { return e.hello; }).indexOf(selectedItem.name);
console.log(pos);
} catch(e) {
$scope.showAlert();
}
};