admin管理员组文章数量:1317898
How do you group an array of objects by an object key to create a new array of objects based on the grouping? For example, I have an array of car objects:
const array = [
{red: [ {height: 50} ]},
{green: [ {height: 20} ]},
{blue: [ {height: 30} ]},
{blue: [ {height: 40} ]},
{red: [ {height: 10} ]},
{green: [ {height: 60} ]}
]
I want to create a new array of objects.(key is color)
const result = [
{red: [{height: 50}, {height: 10}]},
{green: [{height: 20}, {height: 60}]},
{blue: [{height: 30}, {height: 40}]}
]
I tried to use lodash.groupBy, however I don't know how to solve this problem at all.
How do you group an array of objects by an object key to create a new array of objects based on the grouping? For example, I have an array of car objects:
const array = [
{red: [ {height: 50} ]},
{green: [ {height: 20} ]},
{blue: [ {height: 30} ]},
{blue: [ {height: 40} ]},
{red: [ {height: 10} ]},
{green: [ {height: 60} ]}
]
I want to create a new array of objects.(key is color)
const result = [
{red: [{height: 50}, {height: 10}]},
{green: [{height: 20}, {height: 60}]},
{blue: [{height: 30}, {height: 40}]}
]
I tried to use lodash.groupBy, however I don't know how to solve this problem at all.
Share Improve this question edited Sep 12, 2020 at 4:53 sonicmario asked Jan 14, 2019 at 8:06 sonicmariosonicmario 6119 silver badges23 bronze badges 5- The first one is exactly doing what OP was asking suing lodash – mplungjan Commented Jan 14, 2019 at 8:12
- @mplungjan all dupes seem to be grouping by key value, not key name. – Keith Commented Jan 14, 2019 at 8:13
- Hey, It's not duplicate question. Look at my question carefully. – sonicmario Commented Jan 14, 2019 at 8:15
-
Im on mobile atm, but the trick would be to use
Object.keys(item)[0]
to get keyname for grouping purposes. – Keith Commented Jan 14, 2019 at 8:18 - 1 stackoverflow./a/40774906/295783 - seems very close to the accepted answer – mplungjan Commented Jan 14, 2019 at 8:26
2 Answers
Reset to default 6Using array reduce you can iterate data and calculate result object.
const array = [
{ 'red': [ { height: 50 } ] },
{ 'green': [ { height: 20 } ] },
{ 'blue': [ { height: 30 } ] },
{ 'blue': [ { height: 40 } ] },
{ 'red': [ { height: 10 } ] },
{ 'green': [ { height: 60 } ] }
];
const res = array.reduce((acc, element) => {
// Extract key and height value array
const [key, heightValue] = Object.entries(element)[0];
// Get or create if non-exist, and push height value from array, index 0
(acc[key] || (acc[key] = [])).push(heightValue[0]);
return acc;
}, {});
console.log(res);
You can use lodash's _.mergeWith()
to bine the objects with the same key, and then use _.map()
to convert it back to an array:
const array = [{"red":[{"height":50}]},{"green":[{"height":20}]},{"blue":[{"height":30}]},{"blue":[{"height":40}]},{"red":[{"height":10}]},{"green":[{"height":60}]}]
const fn = _.flow([
arr => _.mergeWith({}, ...arr, (o, s) => _.isArray(o) ? o.concat(s) : s),
objs => _.map(objs, (v, k) => ({ [k]: v }))
])
const result = fn(array)
console.log(result)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
本文标签: javascriptHow to group an array of objects by keyStack Overflow
版权声明:本文标题:javascript - How to group an array of objects by key? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742033797a2416949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论