【问题标题】:Merge Object preserving it's value using addition in javascript使用javascript中的加法合并对象以保留其值
【发布时间】:2020-01-25 06:31:28
【问题描述】:

我的 js 对象数组的结构如下所示,

items= [
    {
      discount: 27.6,
      name: 'Floy Vandervort',
      price: 230,
      quantity: 3,
      taxable: 662.4
    },
    {
      discount: 122.88,
      name: 'Adriel Abshire II',
      price: 256,
      quantity: 6,
      taxable: 1413.12
    },
    {
      discount: 159.66,
      name: 'Tabitha Stroman',
      price: 887,
      quantity: 2,
      taxable: 1614.34
    },
    {
      discount: 27.6,
      name: 'Floy Vandervort',
      price: 230,
      quantity: 3,
      taxable: 662.4
    },
    {
      discount: 122.88,
      name: 'Adriel Abshire II',
      price: 256,
      quantity: 6,
      taxable: 1413.12
    },
    {
      discount: 159.66,
      name: 'Tabitha Stroman',
      price: 887,
      quantity: 2,
      taxable: 1614.34
    },
    {
      discount: 27.6,
      name: 'Floy Vandervort',
      price: 230,
      quantity: 3,
      taxable: 662.4
    },
    {
      discount: 122.88,
      name: 'Adriel Abshire II',
      price: 256,
      quantity: 6,
      taxable: 1413.12
    },
    {
      discount: 159.66,
      name: 'Tabitha Stroman',
      price: 887,
      quantity: 2,
      taxable: 1614.34
    }
  ]

我想避免基于name 属性的对象重复。所以我决定通过保留其评估来合并它们,如下所示,

用例

考虑属性name,在上面的数组中 Floy Vandervort 重复 3 次。要将其转换为单个对象,请通过添加保留值将它们合并为单个对象。因此,discountquantitytaxable 属性应通过加法合并,但 price 属性除外。

我正在寻找一个最佳解决方案,我通过迭代原始数组并将合并的对象推送到另一个数组来实现。我想消除复杂性,有可能吗?如果是,如何?这是我正在使用的功能

function(items) {
  let filtered = [];
  items.forEach((item) => {
    if (!isContains(filtered, item)) {
      filtered.push(item);
    } else {
      index = filtered.findIndex((x) => x.name === item.name);
      filtered[index].discount += item.discount;
      filtered[index].quantity += item.quantity;
      filtered[index].taxable += item.taxable;
    }
  });

  return filtered;
}

function isContains(items, ob) {
  items.forEach((item) => {
    if (item.name === ob.name) {
      return true;
    }
  });
  return false;
}

【问题讨论】:

  • @CertainPerformance 我刚刚进行了编辑并放置了我正在使用的代码。
  • @CertainPerformance 这就是我想要的,谢谢。

标签: javascript arrays optimization duplicates array-merge


【解决方案1】:

forEach 回调中返回无效 - isContains 将始终返回 false。最好使用按名称索引的对象或 Map,以将计算复杂度降低一个数量级 - 然后您可以获取该对象的值以获取您想要的数组:

const items=[{discount:27.6,name:"Floy Vandervort",price:230,quantity:3,taxable:662.4},{discount:122.88,name:"Adriel Abshire II",price:256,quantity:6,taxable:1413.12},{discount:159.66,name:"Tabitha Stroman",price:887,quantity:2,taxable:1614.34},{discount:27.6,name:"Floy Vandervort",price:230,quantity:3,taxable:662.4},{discount:122.88,name:"Adriel Abshire II",price:256,quantity:6,taxable:1413.12},{discount:159.66,name:"Tabitha Stroman",price:887,quantity:2,taxable:1614.34},{discount:27.6,name:"Floy Vandervort",price:230,quantity:3,taxable:662.4},{discount:122.88,name:"Adriel Abshire II",price:256,quantity:6,taxable:1413.12},{discount:159.66,name:"Tabitha Stroman",price:887,quantity:2,taxable:1614.34}];

function squish(items) {
  const squishedItemsByName = items.reduce((a, { name, ...props }) => {
    if (!a[name]) {
      a[name] = { name };
    }
    Object.entries(props).forEach(([prop, val]) => {
      a[name][prop] = (a[name][prop] || 0) + val;
    });
    return a;
  }, {});
  return Object.values(squishedItemsByName);
}

console.log(squish(items));

【讨论】:

    【解决方案2】:

    试试

    let h = {};
    items.forEach(x=> h[x.name]= !h[x.name] ? x : {
          discount: h[x.name].discount+x.discount,
          name: x.name,
          price: x.price,
          quantity: h[x.name].quantity+x.quantity,
          taxable: h[x.name].taxable+x.taxable
        });
    
    let result = Object.keys(h).map(k=> h[k]);
    

    items= [
        {
          discount: 27.6,
          name: 'Floy Vandervort',
          price: 230,
          quantity: 3,
          taxable: 662.4
        },
        {
          discount: 122.88,
          name: 'Adriel Abshire II',
          price: 256,
          quantity: 6,
          taxable: 1413.12
        },
        {
          discount: 159.66,
          name: 'Tabitha Stroman',
          price: 887,
          quantity: 2,
          taxable: 1614.34
        },
        {
          discount: 27.6,
          name: 'Floy Vandervort',
          price: 230,
          quantity: 3,
          taxable: 662.4
        },
        {
          discount: 122.88,
          name: 'Adriel Abshire II',
          price: 256,
          quantity: 6,
          taxable: 1413.12
        },
        {
          discount: 159.66,
          name: 'Tabitha Stroman',
          price: 887,
          quantity: 2,
          taxable: 1614.34
        },
        {
          discount: 27.6,
          name: 'Floy Vandervort',
          price: 230,
          quantity: 3,
          taxable: 662.4
        },
        {
          discount: 122.88,
          name: 'Adriel Abshire II',
          price: 256,
          quantity: 6,
          taxable: 1413.12
        },
        {
          discount: 159.66,
          name: 'Tabitha Stroman',
          price: 887,
          quantity: 2,
          taxable: 1614.34
        }
      ]
    
    let h = {};
    items.forEach(x=> h[x.name]= !h[x.name] ? x : {
          discount: h[x.name].discount+x.discount,
          name: x.name,
          price: x.price,
          quantity: h[x.name].quantity+x.quantity,
          taxable: h[x.name].taxable+x.taxable
        });
        
    let result = Object.keys(h).map(k=> h[k]);
    
    console.log(result);

    【讨论】:

      【解决方案3】:

      执行此操作的许多方法之一可能如下:

      const uniqueItems = Object.values(
          items.reduce((hashMap, item) => { 
              if (!hashMap[item.name]) {
                  hashMap[item.name] = { ...item };
              } else {
                  // You should use a safest way to add 
                  hashMap[item.name].discount += item.discount;
                  hashMap[item.name].price += item.price;
                  hashMap[item.name].quantity += item.quantity;
                  hashMap[item.name].taxable += item.taxable;
              }
      
              return hashMap;
          }, {})
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-21
        • 2017-04-19
        • 2012-08-25
        • 1970-01-01
        • 2020-07-04
        • 2014-05-06
        • 1970-01-01
        • 2020-05-03
        相关资源
        最近更新 更多