admin管理员组文章数量:1327674
Is there a simple way to achieve this with lodash?
_.something([{a: 3, b: 4}, {a: 3, b: 5}, {a: 10}], 'a')
=> { 3: [ {a: 3, b: 4}, {a: 3, b: 5 } ], 10: [{ a: 10 }]}
That is, group all the values that share the same key together as an array under that key.
Is there a simple way to achieve this with lodash?
_.something([{a: 3, b: 4}, {a: 3, b: 5}, {a: 10}], 'a')
=> { 3: [ {a: 3, b: 4}, {a: 3, b: 5 } ], 10: [{ a: 10 }]}
That is, group all the values that share the same key together as an array under that key.
Share Improve this question asked Sep 9, 2018 at 11:20 Steve BennettSteve Bennett 127k45 gold badges186 silver badges243 bronze badges1 Answer
Reset to default 9You could use _.groupBy
for grouping by a given key.
Creates an object posed of keys generated from the results of running each element of
collection
thruiteratee
. The order of grouped values is determined by the order they occur incollection
. The corresponding value of each key is an array of elements responsible for generating the key. Theiteratee
is invoked with one argument: (value).
console.log(_.groupBy([{ a: 3, b: 4 }, { a: 3, b: 5 }, { a: 10 }], 'a'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
本文标签: javascriptMultiple version of keyBy in lodash (Group values sharing a key as an array)Stack Overflow
版权声明:本文标题:javascript - Multiple version of keyBy in lodash? (Group values sharing a key as an array) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742225158a2436176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论