Home > OS >  I need to remove duplicate products in an Array and sum their quantity in JavaScript
I need to remove duplicate products in an Array and sum their quantity in JavaScript

Time:03-23

In the program, I fetch arrays of products from orders via API. The array contains a list with products that are duplicated. I need to remove the duplicate products in the array and sum their quantities.

This is what the array I get looks like:

const productsAll = [
  {
    name: 'Product 1',
    ean: '1112223334445',
    sku: '4445',
    product_id: '70604566',
    quantity: 1,
  },
  {
    name: 'Product 1',
    ean: '1112223334445',
    sku: '4445',
    product_id: '70604566',
    quantity: 3,
  },
  {
    name: 'Product 2',
    ean: '1112223334446',
    sku: '4446',
    product_id: '60404533',
    quantity: 2,
  },
  {
    name: 'Product 3',
    ean: '1112223334447',
    sku: '4447',
    product_id: '30504512',
    quantity: 8,
  },
];

I want to make it look like this after removing the duplicates and adding up the quantities:

Product 1 is reduced and the quantity is summed

[
  {
    name: 'Product 1',
    ean: '1112223334445',
    sku: '4445',
    product_id: '70604566',
    quantity: 4,
  },
  {
    name: 'Product 2',
    ean: '1112223334446',
    sku: '4446',
    product_id: '60404533',
    quantity: 2,
  },
  {
    name: 'Product 3',
    ean: '1112223334447',
    sku: '4447',
    product_id: '30504512',
    quantity: 8,
  },
];

CodePudding user response:

You can try the below approach to achieve this. Create an object (productsCheck in my case) and loop through the productsAll array. If the name already exists in the object then add the quantity, else simple add the product to your object ( i.e productsCheck)

Working code:

const productsAll = [{
    name: 'Product 1',
    ean: '1112223334445',
    sku: '4445',
    product_id: '70604566',
    quantity: 1,
  },
  {
    name: 'Product 1',
    ean: '1112223334445',
    sku: '4445',
    product_id: '70604566',
    quantity: 3,
  },
  {
    name: 'Product 2',
    ean: '1112223334446',
    sku: '4446',
    product_id: '60404533',
    quantity: 2,
  },
  {
    name: 'Product 3',
    ean: '1112223334447',
    sku: '4447',
    product_id: '30504512',
    quantity: 8,
  },
];

const productsCheck = {}

productsAll.forEach(product => {
  if (product.name in productsCheck) {
    productsCheck[product.name].quantity  = product.quantity
  } else {
    productsCheck[product.name] = product
  }
})

console.log(productsCheck)

CodePudding user response:

I would suggest using Array.reduce() to create a map of quantities for each product, productQuantities, while removing duplicates.

I'm using product_id for the uniquifying key, one could use another if appropriate.

We'd also sum the totalQuantity in this loop.

const productsAll = [ { name: 'Product 1', ean: '1112223334445', sku: '4445', product_id: '70604566', quantity: 1, }, { name: 'Product 1', ean: '1112223334445', sku: '4445', product_id: '70604566', quantity: 3, }, { name: 'Product 2', ean: '1112223334446', sku: '4446', product_id: '60404533', quantity: 2, }, { name: 'Product 3', ean: '1112223334447', sku: '4447', product_id: '30504512', quantity: 8, }, ];

const productQuantities = productsAll.reduce((acc, el) => {
    const uniqueKey = el.product_id;
    if (!acc[uniqueKey]) {
        acc[uniqueKey] = { name: el.name, quantity: el.quantity };
        acc.totalQuantity = (acc.totalQuantity || 0)   el.quantity;
    }
    return acc;
}, {})

console.log('Product quantities:', productQuantities);

console.log('Total quantity:', productQuantities.totalQuantity)
.as-console-wrapper { max-height: 100% !important; }

CodePudding user response:

Try this simple logic

const productsAll = [{
    name: 'Product 1',
    ean: '1112223334445',
    sku: '4445',
    product_id: '70604566',
    quantity: 1,},
    {
    name: 'Product 1',
    ean: '1112223334445',
    sku: '4445',
    product_id: '70604566',
    quantity: 3,
    },
    {
    name: 'Product 2',
    ean: '1112223334446',
    sku: '4446',
    product_id: '60404533',
    quantity: 2,
    },
    {
    name: 'Product 3',
    ean: '1112223334447',
    sku: '4447',
    product_id: '30504512',
    quantity: 8,
    },
    ];

    const products = {};
    const reducedProducts = [];
      productsAll.forEach((prd,idx) => {
      if (products[prd.product_id]) {
      prd.quantity  = products[prd.product_id].quantity;
      products[prd.product_id].quantit =  prd.quantity;
      reducedProducts[products[prd.product_id].index] = prd; 
      }   
      else {
        products[prd.product_id] = {index:idx,quantity:prd.quantity};
        reducedProducts.push(prd)
      }
      })

     console.log(reducedProducts)

CodePudding user response:

This is an improvement over Terry's answer, because it was not exactly what you ask.

Here you first construct a map using reduce, then convert that map to an array.

const productsAll = [{ name: 'Product 1', ean: '1112223334445', sku: '4445', product_id: '70604566', quantity: 1, }, { name: 'Product 1', ean: '1112223334445', sku: '4445', product_id: '70604566', quantity: 3, }, { name: 'Product 2', ean: '1112223334446', sku: '4446', product_id: '60404533', quantity: 2, }, { name: 'Product 3', ean: '1112223334447', sku: '4447', product_id: '30504512', quantity: 8, },];

const productsMerged = Object.values(productsAll.reduce((acc, el) => {
    const uniqueKey = el.product_id;
    if (!acc[uniqueKey]) {
        acc[uniqueKey] = el;
    } else {
        acc[uniqueKey].quantity  = el.quantity;
    }
    return acc;
}, {}));

console.log('Product quantities:', productsMerged);
  • Related