Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度js复制值推入数组_Javascript_Angularjs - Fatal编程技术网

Javascript 角度js复制值推入数组

Javascript 角度js复制值推入数组,javascript,angularjs,Javascript,Angularjs,我需要停止向数组中插入重复值。我的功能如下。很难在代码中演示整个过程。因此,请检查以下功能 $scope.showTab = []; $scope.goToTabContent = function(id){ $scope.currentId = id; $scope.reportTab.push(id); }; currently I can insert

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

           $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.

如果必须是一个数组,那么避免重复的唯一方法就是不要向数组中已经存在的数组添加值。这意味着首先扫描阵列。(这可能不是很有效,在这种情况下,您可能需要不同的数据结构或算法)
$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);    
    }
};
if ($.inArray(id,$scope.reportTab)==-1) $scope.reportTab.push(id);