angularjs forEach and splice

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

    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);
          }
        });
      }
    });
    

提交回复
热议问题