Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按首字母从数组中查找和删除元素_Javascript_Angularjs_Arrays - Fatal编程技术网

Javascript 按首字母从数组中查找和删除元素

Javascript 按首字母从数组中查找和删除元素,javascript,angularjs,arrays,Javascript,Angularjs,Arrays,我有一个动态数组。值来自方法push的函数,我希望能够检查数组中是否已经有具有相同字母的元素“X”,所以我希望删除此元素,然后推送新元素。我该怎么做? 这是我的代码 代码 已更新 在help@Anton的帮助下,我终于完成了创建正确请求的逻辑。这是有效的。也许它对某些人有用试试这个 //Create new Array and put element on it.. $scope.filter = [] $scope.TempArr = []; var string; $scope.re

我有一个动态数组。值来自方法push的函数,我希望能够检查数组中是否已经有具有相同字母的元素“X”,所以我希望删除此元素,然后推送新元素。我该怎么做? 这是我的代码

代码

已更新 在help@Anton的帮助下,我终于完成了创建正确请求的逻辑。这是有效的。也许它对某些人有用

试试这个

 //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)
   }
  }
 }
//创建新数组并在其上放置元素。。
$scope.filter=[]
$scope.TempArr=[];
var字符串;
$scope.requestSelected=函数(参数){
对于(变量i=0;i<$scope.filter.length;i++){
if($scope.filter[i]==param){
$scope.TempArr.push(参数);
}否则{
$scope.TempArr.push(参数);
}
if(i=$scope.filter.length-1){
//$scope.filter=[];如果需要,则只能更新新元素
$scope.filter=$scope.TempArr;
字符串=$scope.filter.join(“;”)
console.log(字符串)
}
}
}

据我所知,您可以这样做(简化名称和函数调用):


有什么细微差别我没有注意到吗?

您总是在这里添加一个条件
$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)

           };

是否要检查两个元素是否完全匹配,或者一个元素是否包含在另一个元素中?请澄清。将任何问题简化为最重要的部分通常是有用的。您的代码中似乎有很多内容,但并非所有内容都是我们理解问题所必需的。如果你能写一些新的代码,只包含一些基本的东西,那么调试事情和让其他人理解所涉及的问题就会容易得多。@NeilA。正是在这个示例中,我想检查$scope.filter是否已经有一个元素'categoryId'或'eventClassId'匹配参数,并将其删除,然后将新参数添加到$scope.filterarray@Whothehellisthat很抱歉,我已经从示例中删除了很多不必要的代码,只留下了一些基本的东西。我想展示整个过程,我是如何得到最后一个变量字符串的,我相信这对于理解这个问题非常重要。我已经把你的代码放到了我的插件中,控制台日志总是空的。这是forked plunk我真的很抱歉,但是
console.log($scope.filter)
只显示了一个空的数组Check$scope.requestSelected=function(param){console.log(param);}..它总是进入其他位置。请检查顶部的param值,这样你会有一些想法。谢谢你的想法,这对我很有帮助
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);
           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)

           };