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);