_.map(迭代遍历处理数组或对象元素)
语法:
_.map(collection, [iteratee=_.identity])
源代码链接:source
npm包链接:npm package
描述:
创建一个数组, value(值) 是iteratee
(迭代函数)遍历collection
(集合)中的每个元素后返回的结果。 iteratee(迭代函数)调用3个参数:(value, index|key, collection).
lodash 中有许多方法是防止作为其他方法的迭代函数,例如:_.every
,_.filter
,_.map
,_.mapValues
,_.reject
, 和_.some
.
受保护的方法有(注:即这些方法不能使用_.every
,_.filter
,_.map
,_.mapValues
,_.reject
, 和_.some
作为 iteratee 迭代函数参数):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
开始版本:0.1.0
参数:
collection (Array|Object)
: 用来迭代的集合。[iteratee=_.identity] (Array|Function|Object|string)
: 每次迭代调用的函数。
返回值:
(Array)
: 返回新的映射后数组。
例子:
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']
Last updated
Was this helpful?