map source npm
_.map(collection, [iteratee=_.identity])
Creates an array of values by running each element in collection
through
iteratee
. The iteratee is invoked with three arguments:
(value, index|key, collection).
Many lodash methods are guarded to work as iteratees for methods like
_.every
, _.filter
, _.map
, _.mapValues
, _.reject
, and _.some
.
The guarded methods are:
ary
, callback
, curry
, curryRight
, drop
, dropRight
, every
,
fill
, invert
, parseInt
, random
, range
, slice
, some
, sortBy
,
take
, takeRight
, template
, trim
, trimLeft
, trimRight
, uniq
,
and words
Arguments
- collection (Array|Object)
The collection to iterate over.
- [iteratee=_.identity] (Function|Object|string)
The function invoked per iteration.
Returns (Array)
Returns the new mapped array.
Example
function square(n) {
return n * n;
}
_.map([1, 2], square);
// => [3, 6]
_.map({ 'a': 1, 'b': 2 }, square);
// => [3, 6] (iteration order is not guaranteed)
var users = [
{ 'user': 'barney' },
{ 'user': 'fred' }
];
// using the `_.property` callback shorthand
_.map(users, 'user');
// => ['barney', 'fred']