lodash中一些好用的方法总结

189 阅读1分钟

.map(collection, [iteratee=.identity])
参数:1. collection  (Array|Object) : 用来迭代的集合。
2. [iteratee=_.identity]  (Array|Function|Object|string) : 每次迭代调用的函数。
返回:是一个数组
实例1:

var users = [  
{ 'user': 'barney' },  
{ 'user': 'fred' }  
];  
  
// The `_.property` iteratee shorthand.  
//第二个参数可以传字段,_.map可以获取到这个字段所有的值
_.map(users, 'user');  
// => ['barney', 'fred']

实例2:

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)