Last Updated: February 25, 2016
·
756
· ivanbokii

Iterative and reduction array methods

I like Underscore.js library and use functions like _.filter, _.reduce, _.map and others a lot in my day to day programming. I was surprised when I found that ECMAScript 5 adds some similar methods to the Array and you can use them right out of the box:

  • every()
  • filter()
  • forEach()
  • map()
  • some()
  • reduce()
  • reduceRight()

so there is not need to use Underscore if you just need to use the 'map' function

var myLovelyArray = [1, 2, 3, 4];
var result = myLovelyArray.map(function(item) { return item * 2; });

//result equals to [2, 4, 6, 8] now

Iterative methods are available in Internet Explorer 9+, Firefox 2+, Safari 3+, Opera 9.5+, and Chrome.
Reduction methods are available in Internet Explorer 9+, Firefox 3+, Safari 4+, Opera 10.5, and Chrome.

For more details check out the great book 'PROFESSIONAL JavaScript for Web Developers' by Nicholas Zakas.