0

Is there any built-in feature on AngularJS to avoid ng-repeater to receive duplicated entries?

Right now I'm using the following code to prevent it:

$scope.tags = ['black','white','red','yellow','blue'];
$scope.selectedTags = [];    

// textarea value
var words = $scope.message.split(' ');

for(var j = 0; j < words.length; j++) {
    for (var k = 0; k < $scope.selectedTags.length; k++) {
        if ($scope.selectedTags[k].Name == words[j]) {
            contains = true;
        }
    }

    if (!contains)
    {
        $scope.selectedTags.push($scope.tags[i]);
        contains = false;
    }
}
Felipe Miosso
  • 7,309
  • 6
  • 44
  • 55

1 Answers1

2

Angular UI has a unique filter:

Filters out all duplicate items from an array by checking the specified key

Alternatively, if it's just a string array you can filter your array like:

arr.filter(function (e, i, arr) {
    return arr.lastIndexOf(e) === i;
});
Community
  • 1
  • 1
CD..
  • 72,281
  • 25
  • 154
  • 163