对值求和,并将具有相同ID的对象值合并到新数组中

时间:2019-01-16 13:15:01

标签: javascript

我有一个从服务器收到的项目对象。每个项目及其数量都必须与卖方保存在一起,但是,在屏幕上显示时,我需要在一个新的数组中独立于卖方来总计这些值。

 [
    { code: 1, item: 'ball', salesman: 'Braan', quantity: 5, price: 10.00},
    { code: 1, item: 'shoe', salesman: 'Alex', quantity: 5, price: 20.00},
    { code: 1, item: 'ball', salesman: 'Max', quantity: 3, price: 10.00},
    { code: 1, item: 'shirt', salesman: 'Braan', quantity: 5, price: 15.00}
]

所需的输出将是:

[
    { code: 1, item: 'ball', quantity: 8, price: 20.00},
    { code: 1, item: 'shoe', quantity: 5, price: 20.00},
    { code: 1, item: 'shirt', quantity: 5, price: 15.00}
]

6 个答案:

答案 0 :(得分:1)

基本上,您可以重新映射对象以删除salesman键。

const serverData = [{
    code: 1,
    item: 'ball',
    salesman: 'Braan',
    quantity: 5,
    price: 10.00
  },
  {
    code: 1,
    item: 'shoe',
    salesman: 'Alex',
    quantity: 5,
    price: 20.00
  },
  {
    code: 1,
    item: 'ball',
    salesman: 'Max',
    quantity: 3,
    price: 10.00
  },
  {
    code: 1,
    item: 'shirt',
    salesman: 'Braan',
    quantity: 5,
    price: 15.00
  }
]

const result = serverData
  .map((item, i, array) => {
    const defaultValue = {
      code: item.code,
      item: item.item,
      quantity: 0,
      price: 0
    }
    const finalValue = array
      .filter(other => other.item === item.item) //we filter the same items
      .reduce((accum, currentVal) => { //we reduce them into a single entry
        accum.quantity += currentVal.quantity;
        accum.price += currentVal.price;
        return accum;
      }, defaultValue);

    return finalValue;
  })
  .filter((item, thisIndex, array) => { //now our new array has duplicates, lets remove them
    const index = array.findIndex((otherItem, otherIndex) => otherItem.item === item.item && otherIndex !== thisIndex && otherIndex > thisIndex);

    return index === -1
  })

console.log(result)

答案 1 :(得分:0)

简单地使用map函数创建具有减少属性的对象的新数组。

const arr = [
    { code: 1, item: 'ball', salesman: 'Braan', quantity: 5, price: 10.00},
    { code: 1, item: 'shoe', salesman: 'Alex', quantity: 5, price: 20.00},
    { code: 1, item: 'ball', salesman: 'Max', quantity: 3, price: 10.00},
    { code: 1, item: 'shirt', salesman: 'Braan', quantity: 5, price: 15.00}
];

const newArr = arr.map((obj) => {
  return {
    code: obj.code,
    quantity: obj.quantity,
    price: obj.price,
    item: obj.item
  }
})

console.log(newArr)

答案 2 :(得分:0)

尝试一下

og = [
  { code: 1, item: 'ball', salesman: 'Braan', quantity: 5, price: 10.00},
  { code: 1, item: 'shoe', salesman: 'Alex', quantity: 5, price: 20.00},
  { code: 1, item: 'ball', salesman: 'Max', quantity: 3, price: 10.00},
  { code: 1, item: 'shirt', salesman: 'Braan', quantity: 5, price: 15.00}
]

var filtered = {};
og.forEach(d => {
  x = filtered[d.item] || {};

  x = {
    code: d.code, 
    item: d.item, 
    quantity: (x.quantity || 0) + d.quantity, 
    price: (x.price || 0) + d.price
  }

  filtered[d.item] = x;
})


var fin = Object.keys(filtered).map(item => filtered[item])

console.log(fin)

尽管我建议创建一个包含所有物品类型的地图,因为这很容易获得。

答案 3 :(得分:0)

您可以通过按item分组并假定价格相同来减少阵列。

var data = [{ code: 1, item: 'ball', salesman: 'Braan', quantity: 5, price: 10.00 }, { code: 1, item: 'shoe', salesman: 'Alex', quantity: 5, price: 20.00 }, { code: 1, item: 'ball', salesman: 'Max', quantity: 3, price: 10.00 }, { code: 1, item: 'shirt', salesman: 'Braan', quantity: 5, price: 15.00 }],
    result = data.reduce((r, { code, item, quantity, price }) => {
        var temp = r.find(o => o.item === item);
        if (!temp) {
            r.push({ code, item, quantity, price });
        } else {
            temp.quantity += quantity;
        }
        return r;
    }, []);

console.log(result)

答案 4 :(得分:0)

var result = [];
myArray.reduce(function(res, value) {
 if (!res[value.item]) {
res[value.item] = { price: 0,quantity: 0 };
result.push(res[value.item])
}
res[value.item].quantity += value.quantity;
res[value.item].price += value.price;

 return res;
 }, {});

console.log(结果)

答案 5 :(得分:0)

谢谢大家,我是这样解决的:

mergeEqualsItems(items: any[]) {
        const itemsCopy = JSON.parse(JSON.stringify(items));
        const map = new Map();
        const newArr = [];
        itemsCopy.forEach(elem => {
          if (map.get(elem.id)) {
            const elemMap = map.get(elem.id);
            elemMap.quantity = elemMap.quantity + elem.quantity;
            map.set(elem.id, elemMap);
          } else {
            delete elem.salesman;
            map.set(elem.id, elem);
          }
        });
        map.forEach(elem => newArr.push(elem));
        console.log(newArr);
        return newArr;
      }