When/why to use map/reduce over for loops

后端 未结 6 1165
情深已故
情深已故 2020-12-09 17:03

So I am getting into a bit of object manipulation in JavaScript for the first time and I have a question I\'m wondering if anyone could answer.

When I have an object

6条回答
  •  粉色の甜心
    2020-12-09 17:28

    Just bumped into this and found that none of the answers highlights one important difference between for-loop and map as to when to use one over the other:

    1. With map you can't break out of an iteration which you can with for-loop.

    For e.g, you can't do this

    const arr = [5, 6, 9, 4];
    arr.map(elem=>{
       if(elem === 5){
         break; //This is not allowed
      }
    })
    

提交回复
热议问题