angularjs forEach and splice

后端 未结 6 1201
梦如初夏
梦如初夏 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:04

    According to the docs, the iterator function allows a third parameter which is the collection which is being iterated. Splicing that collection instead of $scope.emails will remove the expected obejct.

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

提交回复
热议问题