简体   繁体   中英

Grouping objects using two arrays into new array

I have to group products from the products array based on the season which is given in the orders array . The orders array has the season key-value pair (eg. "season": "2015" ). The products can be mapped to the individual order object with the "id" of the order array and the "orderlineId" of the products array.

When I have the products grouped by season, they also need to be grouped based on "uniqueId" and "colorCode" which is inside product.uniqueId and product.colorCode .

Orders array

{
    "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"
}
]

products array

        "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
    },

I think a new array would be best here so I iterate over them more easily.

desired outcome: new array

      {
        "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
           }
        }
]

I found it quite tricky to do. I assume I should map over the seasons first, but not sure how to group the products and build a new array. Help is much appreciated!

Ah,

I think I found a sufficient answer to my own question. It's not exactly like the desired output, but I can work with it.: I used:

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM