【发布时间】:2021-12-28 05:32:34
【问题描述】:
这是我的例子:
{
id: 'productId',
label: 'productLabel',
items: productSizes.map( productSize => {
return {
id: productSize.groupId,
label: productSize.groupId.split('-')[0],
items: productSize.size,
}
}),
}
这将导致我们的数据如下所示:
{
id: 'productId',
label: 'productLabel'
items: [
{id: 'productA-11', label: 'productA', items: {width: 100, height: 100}},
{id: 'productA-11', label: 'productA', items: {width: 150, height: 150}},
{id: 'productA-11', label: 'productA', items: {width: 200, height: 200}},
{id: 'productB-22', label: 'productB', items: {width: 100, height: 100}},
]
}
但我想得到这样的东西:
{
id: 'productId',
label: 'productLabel'
items: [
{id: 'productA-11', label: 'productA', items: [ {width: 100, height: 100}, {width: 150, height: 150}, {width: 200, height:200}],
{id: 'productB-22', label: 'productB', items: [{width: 100, height: 100}],
]
}
不确定我是否用文字很好地描述了我的问题,但我想以某种方式展平内部属性 items,以便将 SAME productId 的大小合并到一个数组中。
【问题讨论】:
-
到目前为止,您尝试过什么合并它们?你被困在哪里了?请发布您尝试过的代码,以便有人检查并帮助您解决问题。
-
@Nitheesh 我没有代码,因为我不知道如何着手解决这个问题。
标签: javascript arrays ecmascript-6