为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

狐的传说 2023-04-14 16:22:00
如何根据值对数组对象进行分组以创建新的值数组对象输入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            }         ]    },]
查看完整描述

3 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

您可以使用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)


查看完整回答
反对 回复 2023-04-14
?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

你应该像这样使用 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

            }

        ]

    }

 

]


查看完整回答
反对 回复 2023-04-14
?
烙印99

TA贡献1829条经验 获得超13个赞

这是我的答案


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)


查看完整回答
反对 回复 2023-04-14
  • 3 回答
  • 0 关注
  • 98 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信