语法:
Copy _ . map ( collection , [ iteratee = _ . identity ]) 源代码链接: sourcearrow-up-right
npm包链接: npm packagearrow-up-right
描述:
创建一个数组, value(值) 是iteratee(迭代函数)遍历collection(集合)中的每个元素后返回的结果。 iteratee(迭代函数)调用3个参数:(value, index|key, collection) .
lodash 中有许多方法是防止作为其他方法的迭代函数,例如:_.everyarrow-up-right ,_.filterarrow-up-right ,_.maparrow-up-right ,_.mapValuesarrow-up-right ,_.rejectarrow-up-right , 和_.somearrow-up-right .
受保护的方法有(注:即这些方法不能使用_.everyarrow-up-right ,_.filterarrow-up-right ,_.maparrow-up-right ,_.mapValuesarrow-up-right ,_.rejectarrow-up-right , 和_.somearrow-up-right 作为 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): 每次迭代调用的函数。
返回值:
例子:
Copy 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']