title: How to sum a array like a Excel Pivot where two keys must match?
body: <p>I try to sum all &quot;Menge&quot; and &quot;Fehler&quot; values where &quot;Datum&quot; AND &quot;Material&quot; must match. The result should look like an Excel Pivot.</p>
  <p>This is my code so far, but I don't know how to add the second key &quot;Material&quot; that also must match .
  I hope you can understand what I try to explain.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var arr = [{
    "Datum": {
      "date": "2000-01-01 00:00:00.000000",
      "timezone_type": 3,
      "timezone": "Europe/Berlin"
    },
    "Material": "123",
    "Menge": 100,
    "Fehler": 5
  }, {
    "Datum": {
      "date": "2000-01-01 00:00:00.000000",
      "timezone_type": 3,
      "timezone": "Europe/Berlin"
    },
    "Material": "123",
    "Menge": 5,
    "Fehler": 1
  }, {
    "Datum": {
      "date": "2000-01-01 00:00:00.000000",
      "timezone_type": 3,
      "timezone": "Europe/Berlin"
    },
    "Material": "123",
    "Menge": 6,
    "Fehler": 65
  }, {
    "Datum": {
      "date": "2000-01-01 00:00:00.000000",
      "timezone_type": 3,
      "timezone": "Europe/Berlin"
    },
    "Material": "222",
    "Menge": 10,
    "Fehler": 5
  }, {
    "Datum": {
      "date": "2000-01-02 00:00:00.000000",
      "timezone_type": 3,
      "timezone": "Europe/Berlin"
    },
    "Material": "444",
    "Menge": 29,
    "Fehler": 1
  }, {
    "Datum": {
      "date": "2000-01-02 00:00:00.000000",
      "timezone_type": 3,
      "timezone": "Europe/Berlin"
    },
    "Material": "123",
    "Menge": 1,
    "Fehler": 1
  }]
  
  const result = Object.values(arr.reduce((acc, obj) =&gt; {
    const [Datum] = obj.Datum.date.split(' ');
    const Menge = (acc[Datum] ?.Menge + obj.Menge) || obj.Menge;
    const Fehler = (acc[Datum] ?.Fehler + obj.Fehler) || obj.Fehler;
    acc[Datum] = {
      Datum,
      Menge,
      Fehler
    };
    return acc;
  }, {}));
  
  console.log(result)</code></pre>
  </div>
  </div>
  </p>
  <p>But the result should look like:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>[{
      "Datum": "2000-01-01",
      "Material": "123",
      "Menge": 111,
      "Fehler": 71
    },
    {
      "Datum": "2000-01-01",
      "Material": "222",
      "Menge": 10,
      "Fehler": 5
    },
    {
      "Datum": "2000-01-02",
      "Material": "444",
      "Menge": 29,
      "Fehler": 1
    },
    {
      "Datum": "2000-01-02",
      "Material": "123",
      "Menge": 1,
      "Fehler": 1
    }
  ]</code></pre>
  </div>
  </div>
  </p>
  <p>Thank you very much</p>