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

How to group by array object and create array with value key 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
            } 
        ]
    },
    {
        "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
            }
        ]
    }
 
]

您可以使用 reduce 按 promotionId 对促销进行分组,然后映射条目以获得最终结果

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)

这是我的回答

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)

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