0

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!

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.