主题
map
js
_.map(collection, [iteratee=_.identity])
通过运行 collection
至 iteratee
中的每个元素创建一个值数组。迭代器使用三个参数调用:(value, index|key, collection)。
¥Creates an array of values by running each element in collection
thru iteratee
. The iteratee is invoked with three arguments: (value, index|key, collection).
许多 lodash 方法被保护起来,作为 _.every
、_.filter
、_.map
、_.mapValues
、_.reject
和 _.some
等方法的迭代器。
¥Many lodash methods are guarded to work as iteratees for methods like _.every
, _.filter
, _.map
, _.mapValues
, _.reject
, and _.some
.
初始值。ary
、chunk
、curry
、curryRight
、drop
、dropRight
、every
、fill
、invert
、parseInt
、random
、range
、rangeRight
、repeat
、sampleSize
、slice
、some
、sortBy
、split
、take
、takeRight
、template
、trim
、trimEnd
、trimStart
和 words
¥The guarded methods are: ary
, chunk
, curry
, curryRight
, drop
, dropRight
, every
, fill
, invert
, parseInt
, random
, range
, rangeRight
, repeat
, sampleSize
, slice
, some
, sortBy
, split
, take
, takeRight
, template
, trim
, trimEnd
, trimStart
, and words
新增于
¥Since
0.1.0
参数
¥Arguments
collection
(数组|对象):要迭代的集合。¥
collection
(Array|Object): The collection to iterate over.[iteratee=_.identity]
(函数):每次迭代调用的函数。¥
[iteratee=_.identity]
(Function): The function invoked per iteration.
返回
¥Returns
(数组):返回新的映射数组。
¥(Array): Returns the new mapped array.
示例
¥Example
js
function square(n) {
return n * n;
}
_.map([4, 8], square);
// => [16, 64]
_.map({ 'a': 4, 'b': 8 }, square);
// => [16, 64] (iteration order is not guaranteed)
var users = [
{ 'user': 'barney' },
{ 'user': 'fred' }
];
// The `_.property` iteratee shorthand.
_.map(users, 'user');
// => ['barney', 'fred']