Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
I need to remove duplicate products in an Array and sum their quantity in JavaScript

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,
  },
];
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!