javascript - how to push elements to an array without repeating non of them? - Stack Overflow

I have this issue here recorded on a video for you to understand easier. As you can see, I am selecting

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;

};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744772269a4592830.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信