antonyboom antonyboom - 3 months ago 11
Javascript Question

Find and Delete element from an array by first letters

I have a dynamic array. Value comes from function by method push, I want to be able to check if an array already has element "X" with same letters, so I want to remove this element and then push new element. How can I do that?
This is plunker with my code

code

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

}


UPDATED
With help @Anton, I finally finished logic for creating a right request. This is working plunker. Maybe it will be useful for somebody

Answer

You always add a condition here $scope.filter.push(param), instead of that you have to separate different fields for each search field and do something like this:

           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)

           };
Comments