2016-08-19 67 views
1

我有一個動態數組。值來自函數的方法push,我想能夠檢查一個數組是否已經具有相同字母的元素「X」,所以我想刪除這個元素,然後推新元素。我怎樣才能做到這一點? 這是plunker我的代碼從數組中查找並刪除第一個字母的元素

代碼

$scope.selectedCat = []; 
     $scope.selectedCl = []; 

     $scope.updateCategory = function (categoryId) { 

      if ($scope.selectedCat.indexOf(categoryId) > -1) { 
       $scope.selectedCat.splice($scope.selectedCat.indexOf(categoryId), 1); 
      } else { 
       $scope.selectedCat.push(categoryId); 
      } 

      $scope.queryCategory = 'categoryId=in=(' + $scope.selectedCat + ")" 
      // console.log($scope.queryCategory) 
      //Optional, to reload on change. 
      $scope.requestSelected ($scope.queryCategory) 

     }; 

     $scope.updateClass = function (classId) { 

      if ($scope.selectedCl.indexOf(classId) > -1) { 
       $scope.selectedCl.splice($scope.selectedCl.indexOf(classId), 1); 
      } else { 
       $scope.selectedCl.push(classId); 
      } 

      $scope.queryClass = 'eventClassId=in=(' + $scope.selectedCl + ")" 
      // console.log($scope.queryClass) 
      //Optional, to reload on change. 
      $scope.requestSelected ($scope.queryClass) 

     }; 


     $scope.filter = [] 
     var string; 
     $scope.requestSelected = function (param){ 

     if($scope.filter already has an elem with the same letters as param){ 

     // delete this elem then 

     $scope.filter.push(param) 
     } else { 

     $scope.filter.push(param) 

    } 

      // final result 
      string = $scope.filter.join(";") 

      console.log(string) 

     } 

修訂 的幫助@Anton,我終於完成了邏輯創建正確的請求。這工作plunker。也許這將是爲別人有用

+0

是否要檢查兩個元素是完全匹配,或者如果一個元素包含在另一個內?請澄清。 –

+0

將任何問題簡化成最重要的部分通常都是有用的。看起來代碼中有很多事情要做,但並不是所有的事情都需要我們理解這個問題。如果你可以編寫一些新的代碼,那麼它將使調試更容易,並且讓其他人瞭解所涉及的問題。 – Whothehellisthat

+0

@NeilA。完全在示例中,我想檢查$ scope.filter是否已經有一個元素'categoryId'或''eventClassId'與param匹配,並將其刪除,然後將新的參數添加到$ scope.filter array – antonyboom

回答

1

你總是在這裏補充一個條件$scope.filter.push(param),而不是說你必須爲每個搜索字段分隔不同的領域,做這樣的事情:

  var filterOptions = { 
       filterBy: '&$filter=', 
       filterParam: "", 
       classId: '', 
       categoryId: '' 
      }; 

      $scope.selectedCat = []; 
      $scope.selectedCl = []; 

      $scope.updateCategory = function (categoryId) { 

       if ($scope.selectedCat.indexOf(categoryId) > -1) { 
        $scope.selectedCat.splice($scope.selectedCat.indexOf(categoryId), 1); 
       } else { 
        $scope.selectedCat.push(categoryId); 
       } 

      filterOptions.categoryId = 'categoryId=in=(' + $scope.selectedCat + ")" 

      filterOptions.filterParam = filterOptions.classId + ";" + filterOptions.categoryId 
      console.log(filterOptions.filterParam) 
      }; 

      $scope.updateClass = function (classId) { 

       if ($scope.selectedCl.indexOf(classId) > -1) { 
        $scope.selectedCl.splice($scope.selectedCl.indexOf(classId), 1); 
       } else { 
        $scope.selectedCl.push(classId); 
       } 

       filterOptions.classId = 'eventClassId=in=(' + $scope.selectedCl + ")" 

       filterOptions.filterParam = filterOptions.classId + ";" + filterOptions.categoryId 
       console.log(filterOptions.filterParam) 

      }; 
+0

感謝您的想法,這對我很有幫助 – antonyboom

1

嘗試這個

//Create new Array and put element on it.. 
$scope.filter = [] 
$scope.TempArr = []; 
var string; 
$scope.requestSelected = function(param) { 
for (var i = 0; i < $scope.filter.length; i++) { 
    if ($scope.filter[i] == param) { 
    $scope.TempArr.push(param); 
    } else { 
    $scope.TempArr.push(param); 
    } 
    if (i == $scope.filter.length - 1) { 
    //$scope.filter=[]; if you want then you can update new element only 
    $scope.filter = $scope.TempArr; 
    string = $scope.filter.join(";") 

    console.log(string) 
    } 
    } 
} 
+0

我已經將您的代碼我的老闆,控制檯日誌總是空的。這是分叉plunk http://plnkr.co/edit/nAoFPcY34NdRrBUW0WFL?p=preview – antonyboom

+0

Console.log($ scope.filter)告訴我輸出 – Maheshvirus

+0

我真的很抱歉,但'console.log($ scope.filter)'只顯示一個空數組 – antonyboom

0

據我瞭解,你可以這樣做(名稱和函數調用簡化):

var filter = ["abc", "123"], param = "_?!" 

var found = filter.indexOf(param); 
if (found !== -1) { 
    // filter already has an elem with the same letters as param 
    filter.splice(found, 1); 
} 

filter.push(param); 

是否有任何細微之處我錯過了?

相關問題