Javascript 使用两个数组将对象分组到新数组中
我必须根据订单数组中给出的季节对产品数组中的产品进行分组。订单数组具有季节键值对(例如“季节”:“2015”)。可以使用订单数组的“id”和产品数组的“orderlineId”将产品映射到单个订单对象 当我按季节对产品进行分组时,它们还需要根据product.uniqueId和product.colorCode中的uniqueId和product.colorCode进行分组 订单数组Javascript 使用两个数组将对象分组到新数组中,javascript,arrays,json,grouping,Javascript,Arrays,Json,Grouping,我必须根据订单数组中给出的季节对产品数组中的产品进行分组。订单数组具有季节键值对(例如“季节”:“2015”)。可以使用订单数组的“id”和产品数组的“orderlineId”将产品映射到单个订单对象 当我按季节对产品进行分组时,它们还需要根据product.uniqueId和product.colorCode中的uniqueId和product.colorCode进行分组 订单数组 { "id": 99945333, "key": "1", "orderNumber":
{
"id": 99945333,
"key": "1",
"orderNumber": "01",
"season": "2007"
},
{
"id": 99945335,
"key": "1",
"orderNumber": "02",
"season": "2016"
},
{
"id": 99945333,
"key": "2",
"orderNumber": "03",
"season": "2019"
},
{
"id": 99945333,
"key": "3",
"orderNumber": "04",
"season": "2017"
}
]
"orderlineId": 99945333,
"product": {
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945333,
"product": {
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945335,
"product": {
"season": null,
"size": "XXL",
"category: "shirt"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945335,
"product": {
"season": null,
"size": "XXL",
"category: "trouser"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945473,
"product": {
"season": null,
"category: "blouse"
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"season": 2007,
"products": [
{
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
},
{
"season": 2016,
"products": [
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
},
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
}
]
产品阵列
{
"id": 99945333,
"key": "1",
"orderNumber": "01",
"season": "2007"
},
{
"id": 99945335,
"key": "1",
"orderNumber": "02",
"season": "2016"
},
{
"id": 99945333,
"key": "2",
"orderNumber": "03",
"season": "2019"
},
{
"id": 99945333,
"key": "3",
"orderNumber": "04",
"season": "2017"
}
]
"orderlineId": 99945333,
"product": {
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945333,
"product": {
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945335,
"product": {
"season": null,
"size": "XXL",
"category: "shirt"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945335,
"product": {
"season": null,
"size": "XXL",
"category: "trouser"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945473,
"product": {
"season": null,
"category: "blouse"
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"season": 2007,
"products": [
{
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
},
{
"season": 2016,
"products": [
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
},
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
}
]
我认为一个新的数组在这里是最好的,所以我更容易迭代它们
理想结果:新阵列
{
"id": 99945333,
"key": "1",
"orderNumber": "01",
"season": "2007"
},
{
"id": 99945335,
"key": "1",
"orderNumber": "02",
"season": "2016"
},
{
"id": 99945333,
"key": "2",
"orderNumber": "03",
"season": "2019"
},
{
"id": 99945333,
"key": "3",
"orderNumber": "04",
"season": "2017"
}
]
"orderlineId": 99945333,
"product": {
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945333,
"product": {
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945335,
"product": {
"season": null,
"size": "XXL",
"category: "shirt"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945335,
"product": {
"season": null,
"size": "XXL",
"category: "trouser"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945473,
"product": {
"season": null,
"category: "blouse"
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"season": 2007,
"products": [
{
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
},
{
"season": 2016,
"products": [
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
},
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
}
]
我发现这很难做到。我想我应该先绘制季节图,但不确定如何对产品进行分组并构建新阵列。非常感谢您的帮助 啊
我想我找到了我自己问题的充分答案。它与期望的输出不完全一样,但我可以使用它!。我用过:
const sortedSeasons = orders.concat(products).reduce((acc, currentVal) => {
acc[currentVal.season] = Object.assign(acc[currentVal.id] || {}, currentVal);
return acc;
}, {});
Object.keys(sortedSeasons).map(i => sortedSeasons[i]);