使用lodash、下划线或数组函数进行多级/多级分组
Multilevel / multiple level group by using lodash, underscore or array functions
我有以下对象数组,它们是我在 getRawMany 上从 typeOrm 查询输出的:
[{
"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
}];
此输出与 sequelize.js 原始查询相似。
我需要按多个级别进行分组,将内容分组为数组,将产品和广告系列也分组为数组。
但我需要以下输出:
[{
"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)
我有以下对象数组,它们是我在 getRawMany 上从 typeOrm 查询输出的:
[{
"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
}];
此输出与 sequelize.js 原始查询相似。 我需要按多个级别进行分组,将内容分组为数组,将产品和广告系列也分组为数组。
但我需要以下输出:
[{
"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)