Reputation: 397
There is this quite simple .map
function
var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
This makes sense, I perform on numbers
the map function and provide a function that will iterate through on each element in the array.
In this next example (all these taken from mozilla, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) , I thought to myself why would we use Array.prototype.map.call
, maybe they are just demonstrating different ways to accomplish the same goal.
var str = '12345';
Array.prototype.map.call(str, function(x) {
return x;
}).reverse().join('');
So I rewrote this as
var str = '12345';
var stringy = str.map(function(x) {
return x;
}).reverse().join('');
Except I get an error "str.map is not a function(...)" Why is this?
Upvotes: 0
Views: 395
Reputation: 476
This is because str
is a string, and strings do not have the map()
function.
map()
is a function of the array type.
Upvotes: 4