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
4 Answers
Reset to default 4You 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
版权声明:本文标题:javascript - how to push elements to an array without repeating non of them? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744772269a2624410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论