What is the difference between .map, .every, and .forEach?

前端 未结 4 1371
遥遥无期
遥遥无期 2020-12-04 05:26

I\'ve always wondered what the difference between them were. They all seem to do the same thing...

4条回答
  •  我在风中等你
    2020-12-04 05:51

    Another consideration to the above great answers is chaining. With forEach() you can't chain, but with map(), you can.

    For example:

    var arrayNumbers = [3,1,2,4,5];
    
    arrayNumbers.map(function(i) {
        return i * 2
    }).sort();
    

    with .forEach(), you can't do the .sort(), you'll get an error.

提交回复
热议问题