동적 배열이 있습니다. 값은 메서드 푸시에 의한 함수에서 가져옵니다. 배열에 이미 동일한 문자가있는 "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. 아마 누군가에 도움이 될 것입니다
당신이 두 가지 요소가 정확히 일치하거나 경우에 있는지 확인 하시겠습니까을 하나의 요소가 다른 요소 안에 포함되어 있습니까? 명확히하십시오. –
일반적으로 모든 문제를 가장 중요한 부분으로 단순화하는 것이 유용합니다. 코드에 많은 부분이있는 것처럼 보입니다. 그러나 문제의 모든 것을 이해해야합니다. 필수 요소가있는 새로운 코드를 작성하면 디버깅하기가 훨씬 쉬워지고 다른 사람들은 관련된 문제를 이해할 수 있습니다. – Whothehellisthat
@NeilA. 정확히 예를 들어 $ scope.filter에 이미 'categoryId'또는 'eventClassId'요소가 param과 일치하는지 확인하고 제거한 다음 $ scope.filter 배열에 새 param을 추가하십시오. – antonyboom