angularjs forEach and splice

后端 未结 6 1254
梦如初夏
梦如初夏 2021-02-01 18:23

I have an array like this:

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


        
6条回答
  •  生来不讨喜
    2021-02-01 19:07

    Like others have pointed out, the culprit of the code is the array got removed. To get around with angular.forEach, you can try the additive/assignment approach:

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

提交回复
热议问题