1

I'm trying to avoid duplicates. If I send the same name (say, Jason) again, I don't want it to be added to the $scope.people for the second time. How can I check it before adding?

$scope.people = [
  {
    name : "Jason",
    age : 26,
    height : 176,
  },
  {
    name : "Mark",
    age : 34
    height : 190
  }
];

$scope.add = function(name,age,height) {
  // How can I make sure that Jason or Mark won't get added again?
}

If it was a simple array, I would have solved it like below, but this is different.

$scope.add = function (name) {
  if ($scope.people.indexOf(name) == -1) {
    $scope.people.push(name);
  }
};
2
  • 1
    Mhm, what should happend if there is an other "Jason" with age of 26 and height of 176? There are a lot of humans on earth. Maybe you going to use unqiue identifiers called IDs. Commented Feb 6, 2017 at 11:45
  • I only want to check names, there won't be any duplicate names since it's a pre defined list that I keep in the database. Actually they were ID's (so I want to check ID's) but I changed it to names to make it simple. Commented Feb 6, 2017 at 11:48

4 Answers 4

2

You can use AngularJS equals like the following example will show you. This is the common AngularJS way to handle such kind of logic. Well, this solution will completly compare two object instead of only comparing one attribute of an object.

    $scope.add = function(name, age, height) {

        //init
        var found = false;

        //equal logic
        angular.forEach($scope.people, function (people) {
            if (angular.equals(people, {
                    name: name,
                    age: age,
                    height: height
                })) {
                found = true; //set found state
                return; // break angular.forEach()
            }
        });

        //proceed
        if (!found) {
            $scope.people.push({
                name: name,
                age: age,
                height: height
            });
        }
    };
Sign up to request clarification or add additional context in comments.

1 Comment

Aaaaaand it worked. Thank you very much. I'll accept this as an answer in 4 minutes.
1

I got this code snippet from an StackOverflow post which I can't find right now. But this should do the trick:

   function unique(collection, keyname) {
    var output = [],
        keys = [];

    angular.forEach(collection, function (item) {
        var key = item[keyname];
        if (keys.indexOf(key) === -1) {
            keys.push(key);
            output.push(item);
        }
    });
    return output;
};

Usage:

$scope.people = unique(jsonArray, 'name');

1 Comment

Thanks, even though @lin's method worked, I'll look into this, too. Upvoted.
1

$.grep might help you over here

var result = $.grep(people, function(e){ return e.name == x.name; });

result will be an array of matches so you will come to know is there any match or not.

1 Comment

Thanks, even though @lin's method worked, I'll look into this, too. Upvoted.
0

you can use a $filter to easily find existing people like this

$scope.people = [{
    name: "Jason",
    age: 26,
    height: 176,
  }, {
    name: "Mark",
    age: 34,
    height: 190
  }];

  $scope.add = function(name, age, height) {
    if(doesntExist(name)){
      $scope.people.push({name: name, age: age, height: height})
    }
  }

  function doesntExist(name){
    return $filter('filter')($scope.people, {name: name}).length === 0
  }

working plunker http://plnkr.co/edit/4z4ofqTMUH4rMzpoIsI5?p=preview

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.