简体   繁体   中英

Multilevel / multiple level group by using lodash, underscore or array functions

I have below array of objects that was my output on getRawMany from typeOrm query:

[{
    "contents_id": "0e362978-ec0a-4851-9f58-e2ad23ec9ee9",
    "contents_title": "Content mock 1",
    "contents_created_at": "2020-11-28T23:42:23.717Z",
    "campaigns_id": "09e34ed1-0dfb-4723-abfb-eec21fc064bc",
    "campaigns_title": "First campaign title",
    "product_title": "product title one",
    "picture_upload": "image_text1.jpg",
    "views": "1"
},
{
    "contents_id": "0e362978-ec0a-4851-9f58-e2ad23ec9ee9",
    "contents_title": "Content mock 1",
    "contents_created_at": "2020-11-28T23:42:23.717Z",
    "campaigns_id": "09e34ed1-0dfb-4723-abfb-eec21fc064bc",
    "campaigns_title": "First campaign title",
    "product_title": "product title two",
    "picture_upload": "image_text2.jpg",
    "views": "1"
},
{
    "contents_id": "0e362978-ec0a-4851-9f58-e2ad23ec9ee9",
    "contents_title": "Content mock 1",
    "contents_created_at": "2020-11-28T23:42:23.717Z",
    "campaigns_id": "09e34ed1-0dfb-4723-abfb-eec21fc064bc",
    "campaigns_title": "First campaign title",
    "product_title": null,
    "picture_upload": null,
    "views": "1"
},
{
    "contents_id": "5b0bc1f1-62ad-4f3e-b2f6-c117a7b46e21",
    "contents_title": "Content mock 3",
    "contents_created_at": "2020-11-29T10:11:45.563Z",
    "campaigns_id": null,
    "campaigns_title": null,
    "product_title": null,
    "picture_upload": null,
    "views": null
},
{
    "contents_id": "6f211b84-4f76-4da7-9cc7-ab5fe9cc9807",
    "contents_title": "Content mock 2",
    "contents_created_at": "2020-11-29T10:07:55.090Z",
    "campaigns_id": null,
    "campaigns_title": null,
    "product_title": null,
    "picture_upload": null,
    "views": null
},
{
    "contents_id": "d1658dd6-2dd0-4b8b-b9b2-b3fcd1d2310a",
    "contents_title": "Content mock 1",
    "contents_created_at": "2020-11-29T10:07:55.090Z",
    "campaigns_id": null,
    "campaigns_title": null,
    "product_title": null,
    "picture_upload": null,
    "views": null
}];

This output was similar in sequelize.js raw query true. I need to group by multiple level, groupings contents as array, and products and campaings as array too.

But I need the below output:

[{
    "contents_id": "0e362978-ec0a-4851-9f58-e2ad23ec9ee9",
    "contents_title": "Content mock 1",
    "views":1,
    "products":[{
                    "product_title": "product title two",
                    "picture_upload": "image_text2.jpg"
                }],
    "campaigns":[{
                    "campaigns_id": "09e34ed1-0dfb-4723-abfb-eec21fc064bc",
                    "campaigns_title": "First campaign title"
                }]
},
{
    "contents_id": "6f211b84-4f76-4da7-9cc7-ab5fe9cc9807",
    "contents_title": "Content mock 2",
    "products":[],
    "campaigns":[]
},
{
    "contents_id": "5b0bc1f1-62ad-4f3e-b2f6-c117a7b46e21",
    "contents_title": "Content mock 3",
    "products":[],
    "campaigns":[]
}]

So, How can I do it with the best simple way using lodash, underscore or javascript array functions?

Is this what you needed? 'rawContent' is your array to be grouped.

const grouped = rawContent.reduce((r, item) => {
    const key = `${item.contents_id}-${item.contents_title}`;
    r[key] = r[key] || { contents_id : item.contents_id, contents_title:item.contents_title, products: [],campaigns :[]  };
    if(item.product_title) r[key]["products"].push({product_title: item.product_title, picture_upload: item.picture_upload})
    if(item.campaigns_id) r[key]["campaigns"].push({campaigns_id: item.campaigns_id, campaigns_title: item.campaigns_title})
    return r;
  }, {})
  
  const result = Object.values(grouped)
  console.log(result)

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