角度js重复值推入数组

时间:2020-10-26 12:09:30

标签: javascript angularjs

我需要停止重复值插入到数组中。我的功能如下。它很难在代码中演示整个过程。因此请检查波纹管功能。

           $scope.showTab = [];
           $scope.goToTabContent = function(id){
                $scope.currentId = id;
                $scope.reportTab.push(id);    
            };
currently I can insert duplicate value into 'report Tab' array.
ex: [9,9].

 I need to push only unique values. how I do it. can u help me.

1 个答案:

答案 0 :(得分:0)

$scope.goToTabContent = function(id){
    $scope.currentId = id;
    //check if id is not present in $scope.reportTab array and push the id
    if($scope.reportTab.indexOf(id) < 0){
      $scope.reportTab.push(id);    
    }
};

您可以使用 Array.indexOf 或angularjs内置jquery lite,因此如果您希望依赖,也可以使用 jQuery.inArray jQuery

if ($.inArray(id,$scope.reportTab)==-1) $scope.reportTab.push(id);