如何按数组对象分组并使用值键javascript创建数组

如何根据值对数组对象进行分组以创建新的值数组对象


输入


var promotions = [

    {

        "promotionID": 2,

        "id": 1,

        "qty": 1,

        "productID": 1,

        "product": "Nu Milk Tea 330ml",

        "operator": null

    }, {

        "promotionID": 2,

        "id": 2,

        "qty": 2,

        "productID": 2,

        "product": "Product testing 2",

        "operator": 1

    }, {

        "promotionID": 2,

        "id": 3,

        "qty": 3,

        "productID": 3,

        "product": "Golda Coffee Dolce Latte 200ml",

        "operator": 2

    }, {

        "promotionID": 3,

        "id": 4,

        "qty": 8,

        "productID": 54,

        "product": "Masker Skrineer 3ply Motif 5pcs",

        "operator": null

    }, {

        "promotionID": 3,

        "id": 5,

        "qty": 5,

        "productID": 53,

        "product": "Masker Skrineer 1ply Grey 5pcs",

        "operator": 2

    }, {

        "promotionID": 3,

        "id": 6,

        "qty": 5,

        "productID": 52,

        "product": "Oronamin C Drink 120ml",

        "operator": 1

    }]

我想制作一个新的汽车对象数组,这些对象按以下方式分组promotionID


预期产出


[

    {

        "promotionID": 2,

        "data" : [

            {

                "promotionID": 2,

                "id": 1,

                "qty": 1,

                "productID": 1,

                "product": "Nu Milk Tea 330ml",

                "operator": null

            }, {

                "promotionID": 2,

                "id": 2,

                "qty": 2,

                "productID": 2,

                "product": "Product testing 2",

                "operator": 1

            }, {

                "promotionID": 2,

                "id": 3,

                "qty": 3,

                "productID": 3,

                "product": "Golda Coffee Dolce Latte 200ml",

                "operator": 2

            } 

        ]

    },

]


狐的传说
浏览 101回答 3
3回答

BIG阳

您可以使用reducepromotionId 对促销进行分组,然后映射条目以获得最终结果var promotions = [  {    promotionID: 2,    id: 1,    qty: 1,    productID: 1,    product: 'Nu Milk Tea 330ml',    operator: null  },  {    promotionID: 2,    id: 2,    qty: 2,    productID: 2,    product: 'Product testing 2',    operator: 1  },  {    promotionID: 2,    id: 3,    qty: 3,    productID: 3,    product: 'Golda Coffee Dolce Latte 200ml',    operator: 2  },  {    promotionID: 3,    id: 4,    qty: 8,    productID: 54,    product: 'Masker Skrineer 3ply Motif 5pcs',    operator: null  },  {    promotionID: 3,    id: 5,    qty: 5,    productID: 53,    product: 'Masker Skrineer 1ply Grey 5pcs',    operator: 2  },  {    promotionID: 3,    id: 6,    qty: 5,    productID: 52,    product: 'Oronamin C Drink 120ml',    operator: 1  }]const res = Array.from(  promotions    .reduce((acc, promotion) => {      acc.set(        promotion.promotionID,        (acc.get(promotion.promotionID) || []).concat(promotion)      )      return acc    }, new Map)    .entries(),  ([promotionId, data]) => ({ promotionId, data }))console.log(res)

牛魔王的故事

你应该像这样使用 lodash。const _ = require('lodash');let filteredPromotions=_.groupBy(promotions,'promotionID');输出:{  '2': [    {      promotionID: 2,      id: 1,      qty: 1,      productID: 1,      product: 'Nu Milk Tea 330ml',      operator: null    },    {      promotionID: 2,      id: 2,      qty: 2,      productID: 2,      product: 'Product testing 2',      operator: 1    },    {      promotionID: 2,      id: 3,      qty: 3,      productID: 3,      product: 'Golda Coffee Dolce Latte 200ml',      operator: 2    }  ],  '3': [    {      promotionID: 3,      id: 4,      qty: 8,      productID: 54,      product: 'Masker Skrineer 3ply Motif 5pcs',      operator: null    },    {      promotionID: 3,      id: 5,      qty: 5,      productID: 53,      product: 'Masker Skrineer 1ply Grey 5pcs',      operator: 2    },    {      promotionID: 3,      id: 6,      qty: 5,      productID: 52,      product: 'Oronamin C Drink 120ml',      operator: 1    }  ]}要获得准确的输出,请添加这些行:    filteredPropomotions = Object.keys(filteredPropomotions).map((key, index)=> {return{promotionID:key,data:filteredPropomotions[key]}});输出:[    {        "promotionID": 2,        "data" : [            {                "promotionID": 2,                "id": 1,                "qty": 1,                "productID": 1,                "product": "Nu Milk Tea 330ml",                "operator": null            }, {                "promotionID": 2,                "id": 2,                "qty": 2,                "productID": 2,                "product": "Product testing 2",                "operator": 1            }, {                "promotionID": 2,                "id": 3,                "qty": 3,                "productID": 3,                "product": "Golda Coffee Dolce Latte 200ml",                "operator": 2            }         ]    },    {        "promotionID": 3,        "data" : [            {                "promotionID": 3,                "id": 4,                "qty": 8,                "productID": 54,                "product": "Masker Skrineer 3ply Motif 5pcs",                "operator": null            }, {                "promotionID": 3,                "id": 5,                "qty": 5,                "productID": 53,                "product": "Masker Skrineer 1ply Grey 5pcs",                "operator": 2            }, {                "promotionID": 3,                "id": 6,                "qty": 5,                "productID": 52,                "product": "Oronamin C Drink 120ml",                "operator": 1            }        ]    } ]

烙印99

这是我的答案var promotions = [  {    promotionID: 2,    id: 1,    qty: 1,    productID: 1,    product: "Nu Milk Tea 330ml",    operator: null,  },  {    promotionID: 2,    id: 2,    qty: 2,    productID: 2,    product: "Product testing 2",    operator: 1,  },  {    promotionID: 2,    id: 3,    qty: 3,    productID: 3,    product: "Golda Coffee Dolce Latte 200ml",    operator: 2,  },  {    promotionID: 3,    id: 4,    qty: 8,    productID: 54,    product: "Masker Skrineer 3ply Motif 5pcs",    operator: null,  },  {    promotionID: 3,    id: 5,    qty: 5,    productID: 53,    product: "Masker Skrineer 1ply Grey 5pcs",    operator: 2,  },  {    promotionID: 3,    id: 6,    qty: 5,    productID: 52,    product: "Oronamin C Drink 120ml",    operator: 1,  },];const objectPromotion = {};for (index in promotions) {  const dataPromotion = objectPromotion[promotions[index].promotionID];  console.log("dataPromotion", dataPromotion)  if (dataPromotion) {    dataPromotion.push(promotions[index]);  } else {    objectPromotion[promotions[index].promotionID] = [promotions[index]];  }}const result = Object.keys(objectPromotion).map((item) => {  return {    promotionID: item,    data: objectPromotion[item],  };});console.log("result", result)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript