How can I remove an element from a dynamic array if it already contains an element with the same letters, and then push a new element? Here is my 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 from @Anton, I have completed the logic for creating a correct request. This code is now working in the updated plunker. Hopefully, this will be useful for someone.