【发布时间】:2021-07-03 14:40:25
【问题描述】:
我有这个对象数组,我需要对其进行修改以使其更容易渲染。
const items = [
{
tab: 'Results',
section: '2017',
title: 'Full year Results',
description: 'Something here',
},
{
tab: 'Results',
section: '2017',
title: 'Half year Results',
description: 'Something here',
},
{
tab: 'Reports',
section: 'Marketing',
title: 'First Report',
description: 'Something here',
},
...
];
我正在尝试修改它,按特定键对它们进行分组。这个想法是有这个输出。如您所见,键的名称可能与项目中的实际名称不同。我认为这与以前的帖子有点不同。
const output = [
{
tab: 'Results',
sections: [
{
section: '2017',
items: [ { 'item that belongs here' }, { ... } ],
},
},
{
tab: 'Reports',
sections: [
{
section: 'Marketing',
items: [ { ... }, { ... } ],
},
},
...
]
我尝试使用lodash.groupby,但它并不能完全满足我的要求。
关于如何处理它的任何想法?
非常感谢!!
【问题讨论】:
-
你想分组哪个键?
标签: javascript arrays object nested grouping