admin管理员组

文章数量:1391747

I have this issue here recorded on a video for you to understand easier. As you can see, I am selecting some elements from an array and pushing those elements to another array named $scope.favoriteLeague = []; in order to do a favorite list of leagues, in that video I am pushing the element as many times as I want and I do not want that, I want to be able to choose only once every element to be on the list, and if the user tries to choose an element that is already on the favorites array, then shows a message.

I am using lodash and angular, this is the code:

$scope.favoriteLeague = []; 

$scope.addToFavorites = function(league) {

   $scope.favoriteLeague.push(league);

};

html

<ion-item ng-repeat="league in favoriteLeague">

    {{league.name}}

</ion-item>

I have this issue here recorded on a video for you to understand easier. As you can see, I am selecting some elements from an array and pushing those elements to another array named $scope.favoriteLeague = []; in order to do a favorite list of leagues, in that video I am pushing the element as many times as I want and I do not want that, I want to be able to choose only once every element to be on the list, and if the user tries to choose an element that is already on the favorites array, then shows a message.

I am using lodash and angular, this is the code:

$scope.favoriteLeague = []; 

$scope.addToFavorites = function(league) {

   $scope.favoriteLeague.push(league);

};

html

<ion-item ng-repeat="league in favoriteLeague">

    {{league.name}}

</ion-item>
Share Improve this question asked Feb 10, 2015 at 15:39 NonNon 8,61920 gold badges80 silver badges130 bronze badges 1
  • 1 Maybe look through the array to see if it already contains league before calling .push()? Maybe with .indexOf()? – Pointy Commented Feb 10, 2015 at 15:45
Add a ment  | 

4 Answers 4

Reset to default 4

You should check that it is not in the array. @itcouldevenbeaboat was close but it should have been === -1 instead of !== -1

$scope.addToFavorites = function(league) {

    if ($scope.favoriteLeague.indexOf(league) === -1){
        $scope.favoriteLeague.push(league);
    }

};

The .indexOf() method is what you ar probably looking for. Wrap this around the code where addToFavorites is called (that way, you don't even call the function, if the value already exists):

if ($scope.favoriteLeague.indexOf(league) === -1) {

    . . . your existing call to addToFavorites . . .

}

Alternately, if you are using jQuery, you can use the $.inArray() method to do the same thing.

actually I got my own solution base on @itcouldevenbeaboat answers

$scope.addToFavorites = function(league) {
  if ($scope.favoriteLeague.indexOf(league) === -1) {
    $scope.favoriteLeague.push(league);
  }else {
    console.log('already exists!!!!!');
  }
};

Just check to see if the array contains the league before pushing it.

$scope.addToFavorites = function(league) {

    $scope.favoriteLeague.indexOf(league) === -1 ? $scope.favoriteLeague.push(league) : void 0;

};

本文标签: javascripthow to push elements to an array without repeating non of themStack Overflow