簡體   English   中英

使用 lodash、下划線或數組函數的多級/多級組

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

我有以下對象數組,即來自 typeOrm 查詢的 getRawMany 上的 output:

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

這個 output 在 sequelize.js 原始查詢中是相似的。 我需要按多個級別分組,將內容分組為數組,並將產品和campaings 分組為數組。

但我需要以下 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":[]
}]

那么,如何使用 lodash、下划線或 javascript 數組函數以最簡單的方式做到這一點?

這是你需要的嗎? 'rawContent' 是要分組的數組。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM