_.keyBy
Lodash only
Creates an object composed of keys generated from the results of running each element of collection thru iteratee.
- // Lodash
- console.log(_.keyBy(['a', 'b', 'c']))
- // output: { a: 'a', b: 'b', c: 'c' }
- console.log(_.keyBy([{ id: 'a1', title: 'abc' }, { id: 'b2', title: 'def' }], 'id')
- // output: { a1: { id: 'a1', title: 'abc' }, b2: { id: 'b2', title: 'def' } }
- console.log(_.keyBy({ data: { id: 'a1', title: 'abc' }}, 'id')
- // output: { a1: { id: 'a1', title: 'abc' }}
- // keyBy for array only
- const keyBy = (array, key) => (array || []).reduce((r, x) => ({ ...r, [key ? x[key] : x]: x }), {});
- // Native
- console.log(keyBy(['a', 'b', 'c']))
- // output: { a: 'a', b: 'b', c: 'c' }
- console.log(keyBy([{ id: 'a1', title: 'abc' }, { id: 'b2', title: 'def' }], 'id')
- // output: { a1: { id: 'a1', title: 'abc' }, b2: { id: 'b2', title: 'def' } }
- console.log(keyBy(Object.values({ data: { id: 'a1', title: 'abc' }}), 'id')
- // output: { a1: { id: 'a1', title: 'abc' }}
- // keyBy for array and object
- const collectionKeyBy = (collection, key) => {
- const c = collection || {};
- return c.isArray() ? keyBy(c, key) : Object.values(keyBy(c, key));
- }
Browser Support for Array.prototype.reduce()
✔ | 12.0 ✔ | 3.0 ✔ | 9.0 ✔ | 10.5 ✔ | 4.0 ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .