Mapping an Array in JavaScript
Another useful feature of ECMAScipt 5 is its Array extensions. I see a lot of
people including
underscore.js in their
Node projects just to have access to _.map
and _.each
. I love underscore, but
in V8 and ECMAScript 5 browsers, you already have these methods (and more)!
Array#each used to be a for loop:
var number;
for (var i = 0; i < numbers.length; i++) {
number = numbers[i];
// do something
}
now we have forEach:
numbers.forEach(function (number) {
// do something
});
And map used to be pretty ugly:
var newNumbers = Array.new(numbers.length);
var number;
for (var i = 0; i < numbers.length; i++) {
number = numbers[i];
newNumbers[i] = someOperation(number);
}
But not anymore:
var newNumbers = numbers.map(function (number) {
return someOperation(number);
});