ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## keyBy + [link](./keyBy "Link to this entry.") + [source](https://github.com/lodash/lodash/blob/4.5.0正式版/lodash.src.js#L7856 "View in source.") + [npm](https://www.npmjs.com/package/lodash.keyby "See the npm package.") ``` _.keyBy(collection, [iteratee=_.identity]) ``` 创建一个对象组成。key 是经 `iteratee` 处理的结果,value 是产生key的元素。 iteratee 会传入1个参数:(value)。 ### 参数 1. collection (Array|Object) 需要遍历的集合 2. [iteratee=_.identity] (Function|Object|string) 这个函数会处理每一个元素 ### 返回值 (Object) 返回一个组成汇总的对象 ### 示例 ``` var array = [ { 'dir': 'left', 'code': 97 }, { 'dir': 'right', 'code': 100 } ]; _.keyBy(array, function(o) { return String.fromCharCode(o.code); }); // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } } _.keyBy(array, 'dir'); // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } } ```