我需要删除数组中的重复产品并将它们的数量相加 JavaScript

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

在程序中,我通过 API 从订单中获取产品数组。该数组包含一个包含重复产品的列表。 我需要删除数组中的重复产品并对它们的数量求和。

这是我得到的数组的样子:

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

我想在删除重复项并添加数量后使其看起来像这样:

商品1减少,数量相加

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

您可以尝试以下方法来实现。创建一个对象(在我的例子中是 productsCheck)并遍历 productsAll 数组。如果名称已存在于对象中,则添加数量,否则简单地将产品添加到您的对象中(即 productsCheck

工作代码:

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)

我建议使用 Array.reduce() 为每个产品创建数量图,productQuantities,同时删除重复项。

我正在使用 product_id 作为统一密钥,如果合适的话可以使用另一个。

我们还将在此循环中对 totalQuantity 求和。

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

试试这个简单的逻辑

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)

这是对特里答案的改进,因为它与您的要求不完全相同。

在这里,您首先使用 reduce 构造一个映射,然后将该映射转换为数组。

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

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) => {
        if (el.name in acc) {
            acc[el.name].quantity = el.quantity + acc[el.name].quantity
        } else {
            acc[el.name] = el;
        }
        
        return acc;
    }, {})
    
    console.log('Product quantities:', productQuantities);