从对象数组获取属性值的总和

时间:2018-07-22 05:13:51

标签: javascript arrays

我如何计算所有装运后每个项目的总数量?我不确定如何计算itemName字符串的出现次数以及如何从发票行中提取quantity值。我可以自己将数量的值映射到一个数组中,但是我不知道如何使用字符串来实现。

 var Shipments = [
    {
        id: 1,
        Invoice: [
            { itemName: "Item 01", quantity: 14 },
            { itemName: "Item 02", quantity: 8 },
            { itemName: "Item 03", quantity: 16 },
            { itemName: "Item 05", quantity: 10 },
        ],
    },
    {
        id: 2,
        Invoice: [
            { itemName: "Item 01", quantity: 1 },
            { itemName: "Item 02", quantity: 42 },
            { itemName: "Item 04", quantity: 13 },
            { itemName: "Item 06", quantity: 37 },
        ],
    },
    {
        id: 3,
        Invoice: [
            { itemName: "Item 3", quantity: 35 },
        ],
    }];

3 个答案:

答案 0 :(得分:0)

这将获得具有相同名称的所有项目的总和。为了清楚起见,请使用代码而不是图片更新您的问题。

https://codesandbox.io/s/0m85k800mp

import { assert } from "chai";

const orders = {
  order1: [
    { name: "item1", quantity: 1 },
    { name: "item2", quantity: 2 },
    { name: "item3", quantity: 3 }
  ],
  order2: [
    { name: "item2", quantity: 1 },
    { name: "item3", quantity: 2 },
    { name: "item4", quantity: 3 }
  ],
  order3: [
    { name: "item3", quantity: 4 },
    { name: "item4", quantity: 5 },
    { name: "item5", quantity: 6 }
  ],
  order4: [
    { name: "item1", quantity: 4 },
    { name: "item2", quantity: 5 },
    { name: "item5", quantity: 6 }
  ]
};

const countObj = {};
Object.keys(orders).forEach(key => {
  orders[key].forEach(item => {
    if (item.name in countObj) {
      countObj[item.name] += item.quantity;
    } else {
      countObj[item.name] = item.quantity;
    }
  });
});

console.log(countObj);

assert.equal(5, countObj.item1, "item1 failed");
assert.equal(8, countObj.item2, "item2 failed");
assert.equal(9, countObj.item3, "item3 failed");
assert.equal(8, countObj.item4, "item4 failed");
assert.equal(12, countObj.item5, "item5 failed");

答案 1 :(得分:0)

var sumOfItems = Shipments.reduce(function(acc, item) {
  item.Invoice.forEach(function(i) {
    //                set to 0 if it is seen for the first time.
    acc[i.itemName] = (acc[i.itemName] || 0) + i.quantity;
  });
  return acc;
}, {});

我们使用reduce函数,然后对发票数量求和

答案 2 :(得分:0)

根据当前的数据结构,您可以在Array.prototype.forEach中使用Array.prototype.reduce。使用accumulator对象跟踪itemName并添加quantity值:

function shipmentsPerItem(shipmentData) {
  return shipmentData.reduce(function(accumulator, shipment) {
    var invoice = shipment.Invoice;
    invoice.forEach(function(el) {
      if (!accumulator[el.itemName]) {
        accumulator[el.itemName] = 0;
      }
      accumulator[el.itemName] += el.quantity;
    });
    return accumulator;
  }, {});
}

var Shipments = [{
    id: 1,
    Invoice: [{
        itemName: "Item 01",
        quantity: 1
      },
      {
        itemName: "Item 02",
        quantity: 3
      },
      {
        itemName: "Item 03",
        quantity: 25
      },
      {
        itemName: "Item 04",
        quantity: 12
      },
    ],
  },
  {
    id: 2,
    Invoice: [{
        itemName: "Item 01",
        quantity: 1
      },
      {
        itemName: "Item 08",
        quantity: 42
      },
      {
        itemName: "Item 09",
        quantity: 13
      },
      {
        itemName: "Item 12",
        quantity: 37
      },
    ],
  },
  {
    id: 3,
    Invoice: [{
      itemName: "Item 12",
      quantity: 16
    }, ],
  },
  {
    id: 4,
    Invoice: [{
        itemName: "Item 10",
        quantity: 11
      },
      {
        itemName: "Item 11",
        quantity: 10
      },
    ],
  },
  {
    id: 5,
    Invoice: [{
        itemName: "Item 06",
        quantity: 7
      },
      {
        itemName: "Item 07",
        quantity: 2
      },
      {
        itemName: "Item 12",
        quantity: 14
      },
    ],
  },
  {
    id: 6,
    Invoice: [{
      itemName: "Item 05",
      quantity: 17
    }, ],
  },
  {
    id: 7,
    Invoice: [{
        itemName: "Item 03",
        quantity: 5
      },
      {
        itemName: "Item 07",
        quantity: 2
      },
    ],
  },
  {
    id: 8,
    Invoice: [{
        itemName: "Item 02",
        quantity: 13
      },
      {
        itemName: "Item 07",
        quantity: 7
      },
      {
        itemName: "Item 09",
        quantity: 2
      },
    ],
  },
  {
    id: 9,
    Invoice: [{
        itemName: "Item 01",
        quantity: 4
      },
      {
        itemName: "Item 06",
        quantity: 17
      },
      {
        itemName: "Item 07",
        quantity: 3
      },
    ],
  },
  {
    id: 10,
    Invoice: [{
        itemName: "Item 11",
        quantity: 12
      },
      {
        itemName: "Item 12",
        quantity: 1
      },
    ],
  },
];

console.log(shipmentsPerItem(Shipments));