How to group by array object and create array with value key javascript

439 views Asked by At

How to group by array object from values to create new array object of values

Input

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
    }]

I want to make a new array of car objects that's grouped by promotionID

Expected Output

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

There are 3 answers

6
hgb123 On BEST ANSWER

You could use reduce to group promotion by promotionId and then map through the entries to get the final result

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)

0
Anhdevit On

This is my answer

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)

0
shakir ullah On

you should use lodash like this.

const _ = require('lodash');
let filteredPromotions=_.groupBy(promotions,'promotionID');

output:

{
  '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
    }
  ]
}

to get exact output add those lines:

    filteredPropomotions = Object.keys(filteredPropomotions).map((key, index)=> {return{promotionID:key,data:filteredPropomotions[key]}}
);

output:

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