2016-08-19 4 views
1

동적 배열이 있습니다. 값은 메서드 푸시에 의한 함수에서 가져옵니다. 배열에 이미 동일한 문자가있는 "X"요소가 있는지 확인할 수 있기를 원합니다.이 요소를 제거한 다음 새 요소를 푸시합니다. 어떻게해야합니까? 이 도움 @Anton으로 내 코드배열에서 첫 번째 문자로 요소 찾기 및 삭제

코드

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

     } 

업데이트]plunker, 나는 마침내 권리 요구를 만들기위한 논리를 완료합니다. 이것은 작동 중입니다 plunker. 아마 누군가에 도움이 될 것입니다

+0

당신이 두 가지 요소가 정확히 일치하거나 경우에 있는지 확인 하시겠습니까을 하나의 요소가 다른 요소 안에 포함되어 있습니까? 명확히하십시오. –

+0

일반적으로 모든 문제를 가장 중요한 부분으로 단순화하는 것이 유용합니다. 코드에 많은 부분이있는 것처럼 보입니다. 그러나 문제의 모든 것을 이해해야합니다. 필수 요소가있는 새로운 코드를 작성하면 디버깅하기가 훨씬 쉬워지고 다른 사람들은 관련된 문제를 이해할 수 있습니다. – Whothehellisthat

+0

@NeilA. 정확히 예를 들어 $ scope.filter에 이미 'categoryId'또는 'eventClassId'요소가 param과 일치하는지 확인하고 제거한 다음 $ scope.filter 배열에 새 param을 추가하십시오. – antonyboom

답변

1

:

  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

코드를 넣었습니다. 내 plunker, 콘솔 로그 항상 비어 있습니다. 이것은 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); 

어떤 뉘앙스가 있습니까 내가 그리워?

당신은 항상 당신이 각 검색 필드에 다른 필드를 구분하고 같은 것을 할 필요가 그 대신에, 조건 여기 $scope.filter.push(param)를 추가