【问题标题】:angularjs forEach and spliceangularjs forEach 和拼接
【发布时间】:2014-08-03 20:58:34
【问题描述】:

我有一个这样的数组:

$scope.emails = [
  {"key":"Work","value":"user@domine.com"},
  {"key":"","value":""},
   {"key":"Work","value":"user2@domine.com"}
  {"key":"","value":""}];

所以,我想删除空电子邮件,但角度 forEach 方法只删除最后一个对象为什么???。

js 代码

angular.forEach($scope.emails, function(email, index){
     if(email.value ===""){
       $scope.emails.splice(index, 1);

     } 

    });

我哪里做错了

JS Bin

【问题讨论】:

  • 请在您的问题中包含相关代码,而不是仅仅链接到 JSBin。

标签: javascript angularjs array-splice


【解决方案1】:

我没有在 AngularJs 上尝试过这个,但是在 Angular 4 中,类似的方法效果很好。

angular.forEach($scope.emails, function(email){
 if(email.value ===""){
   $scope.emails.splice($scope.emails.indexOf(email), 1);
 } 

});

Angular 4 版本:

this.emailArray.forEach(email => {
  if (email.value == "") {
    this.emailArray.splice(this.emailArray.indexOf(email),1);
  }
});

【讨论】:

    【解决方案2】:

    indexOf 在找不到项目时返回-1

    删除一个项目并避免在未找到时删除最后一个项目的方法是:

    var index = $scope.items.indexOf($scope.oldItem);
    
    if (index != -1) {
      $scope.items.splice(index, 1);
    }
    

    【讨论】:

      【解决方案3】:

      就像其他人指出的那样,代码的罪魁祸首是数组被删除了。要解决 angular.forEach,您可以尝试加法/赋值方法:

      var filteredEmails = [];
      angular.forEach($scope.emails, function(email, index){
          if(email.value !==""){
              filteredEmails.push(email);
          }
      });
      
      $scope.emails = filteredEmails;
      

      【讨论】:

        【解决方案4】:
        describe('Foreach Splice', function () {
          it('splicing', function () {
        
            var elements = [
              {name: "Kelly", age: 16},
              {name: "", age: 17},
              {name: "Becky", age: 18},
              {name: "", age: 18},
              {name: "Sarah", age: 19},
              {name: "", age: 20},
              {name: "", age: 22},
              {name: "Mareck", age: 21},
              {name: "", age: 21},
              {name: "Mareck", age: 21}
            ];
        
            removeEmptyEntry(elements);
            console.log(elements);
          });
        
        
          function removeEmptyEntry(elements) {
            elements.forEach(function (element, index) {
              if (!element.name) {
                elements.splice(index, 1);
                removeEmptyEntry(elements);
              }
            });
          }
        });
        

        【讨论】:

        • 只是我想删除空值而不是第二个索引元素
        • 啊你的意思是你想删除空元素?
        【解决方案5】:

        问题是您在循环期间从数组中删除元素,因此后面的项目位于不同的索引处。您需要向后循环:

        for (var i = $scope.emails.length - 1; i >= 0; i--) {
            if (!$scope.emails[i].value) {
                $scope.emails.splice(i, 1);
            }
        }
        

        这是updated example

        【讨论】:

        • splice 的第二个参数表示要删除多少个元素,所以如果您之前可以确定,也可以在一个 splice 中完成。例如,您可以使用$scope.emails.splice(1,2)(或$scope.emails.splice(1),这将删除第一个元素之后的所有内容)删除数组的最后两封电子邮件。
        • @James Allardice 感谢您的重播。它工作正常。但我的问题是我们不能使用 forEach 来做到这一点
        • @chandu - 不,forEach 以错误的方向迭代数组,因此您必须使用正常循环。
        • @link - 只有当有问题的元素在数组中彼此相邻时才会起作用。在这种情况下很好,但它不是很健壮。
        • @JamesAllardice 当然,这就是为什么我添加了“如果你之前可以确定”:) OP 的问题不是很清楚,所以我想添加它作为备注,以防他的情况允许。
        猜你喜欢
        • 2014-04-21
        • 1970-01-01
        • 1970-01-01
        • 2015-07-14
        • 1970-01-01
        • 2010-12-24
        • 2018-04-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多