How to remove element from array in forEach loop?

前端 未结 7 518
耶瑟儿~
耶瑟儿~ 2020-11-28 03:37

I am trying to remove an element in an array in a forEach loop, but am having trouble with the standard solutions I\'ve seen.

This is what I\'m current

7条回答
  •  無奈伤痛
    2020-11-28 03:58

    Use Array.prototype.filter instead of forEach:

    var pre = document.getElementById('out');
    
    function log(result) {
      pre.appendChild(document.createTextNode(result + '\n'));
    }
    
    var review = ['a', 'b', 'c', 'b', 'a', 'e'];
    review = review.filter(item => item !== 'a');
    log(review);
    

提交回复
热议问题