title: Segregate array of object by id, then find sum of all amount fields and push all string elements to array
body: <p>I have an array of object and have to segregate based on id, then sum up all amount fields, push all string to an array and keep date fields empty.</p>
  <p>I have tried to segregate the data but failed to find the sum of amount fields and other data.</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>givenData = [
      { id: 'ABC001', code: 'AL', accountNo: '57808', debtDate: '2022-02-01T00:00:00.000Z', debtAmt: 20000, totalPaid: 100, principalDue: 5000, principalPaid: 20, interestDue: 1000, interestPaid: 20, penaltyDue: 5000, penaltyPaid: 150 },
      { id: 'ABC001', code: 'AL', accountNo: '57808', debtDate: '2022-02-01T00:00:00.000Z', debtAmt: 30000, totalPaid: 200, principalDue: 4000, principalPaid: 30, interestDue: 5000, interestPaid: 20, penaltyDue: 5000, penaltyPaid: 250 },
      { id: 'ABC001', code: 'HL', accountNo: '95783', debtDate: '2022-02-22T03:56:19.517Z', debtAmt: 2000, totalPaid: 300, principalDue: '', principalPaid: 50, interestDue: '', interestPaid: 10, penaltyDue: 4000, penaltyPaid: '' },
      { id: 'ABC001', code: 'HL', accountNo: '60092', debtDate: '2022-02-25T00:00:00.000Z', debtAmt: 3000, totalPaid: 400, principalDue: 3000, principalPaid: 80, interestDue: 9000, interestPaid: 20, penaltyDue: 3000, penaltyPaid: 200 },
  
      { id: 'DEF123', code: 'HL', accountNo: '480001', debtDate: '2022-03-04T09:51:14.899Z', debtAmt: 2000, totalPaid: '', principalDue: 2000, principalPaid: 10, interestDue: 1000, interestPaid: 50, penaltyDue: 2000, penaltyPaid: 100 },
      { id: 'DEF123', code: 'DL', accountNo: '480002', debtDate: '2022-03-04T09:51:14.899Z', debtAmt: 3000, totalPaid: '', principalDue: 500, principalPaid: 20, interestDue: 500, interestPaid: 100, penaltyDue: 1000, penaltyPaid: 150 },
      { id: 'DEF123', code: 'DL', accountNo: '480003', debtDate: '2022-03-04T09:51:14.899Z', debtAmt: 4000, totalPaid: 100, principalDue: 100, principalPaid: 30, interestDue: 200, interestPaid: 200, penaltyDue: 500, penaltyPaid: 250 },
      { id: 'DEF123', code: 'DL', accountNo: '480004', debtDate: '2022-03-04T09:51:14.899Z', debtAmt: 5000, totalPaid: 200, principalDue: '', principalPaid: 40, interestDue: 300, interestPaid: 300, penaltyDue: '', penaltyPaid: 300 },
      { id: 'DEF123', code: 'AL', accountNo: '480005', debtDate: '2022-03-04T10:52:18.161Z', debtAmt: 6000, totalPaid: 500, principalDue: 50, principalPaid: 60, interestDue: 400, interestPaid: 500, penaltyDue: 100, penaltyPaid: 500 },
  
      { id: 'XYZ001', code: 'PL', accountNo: '878210', debtDate: '2021-09-21T11:38:53.733Z', debtAmt: 500, totalPaid: 600, principalDue: 800, principalPaid: 200, interestDue: 900, interestPaid: 500, penaltyDue: 200, penaltyPaid: 100 },
      { id: 'XYZ001', code: 'DL', accountNo: '878211', debtDate: '2021-09-21T11:38:53.733Z', debtAmt: 700, totalPaid: 900, principalDue: 700, principalPaid: 100, interestDue: 300, interestPaid: '', penaltyDue: '', penaltyPaid: '' },
  ]
  
  expectedResult = [
      {
          id: 'ABC001', code: ['AL', 'HL'], accountNo: ['57808', '95783', '60092'], debtAmt: 55000, totalPaid: 1000, principalDue: 12000, principalPaid: 180, interestDue: 15000, interestPaid: 70, penaltyDue: 17000, penaltyPaid: 500,
          subRows: [
              { id: 'ABC001', code: 'AL', accountNo: '57808', debtDate: '2022-02-01T00:00:00.000Z', debtAmt: 20000, totalPaid: 100, principalDue: 5000, principalPaid: 20, interestDue: 1000, interestPaid: 20, penaltyDue: 5000, penaltyPaid: 150 },
              { id: 'ABC001', code: 'AL', accountNo: '57808', debtDate: '2022-02-01T00:00:00.000Z', debtAmt: 30000, totalPaid: 200, principalDue: 4000, principalPaid: 30, interestDue: 5000, interestPaid: 20, penaltyDue: 5000, penaltyPaid: 250 },
              { id: 'ABC001', code: 'HL', accountNo: '95783', debtDate: '2022-02-22T03:56:19.517Z', debtAmt: 2000, totalPaid: 300, principalDue: '', principalPaid: 50, interestDue: '', interestPaid: 10, penaltyDue: 4000, penaltyPaid: '' },
              { id: 'ABC001', code: 'HL', accountNo: '60092', debtDate: '2022-02-25T00:00:00.000Z', debtAmt: 3000, totalPaid: 400, principalDue: 3000, principalPaid: 80, interestDue: 9000, interestPaid: 20, penaltyDue: 3000, penaltyPaid: 200 }
          ]
      },
  
      {
          id: 'DEF123', code: ['HL', 'DL', 'AL'], accountNo: ['480001', '480002', '480003', '480004', '480005'], debtAmt: 20000, totalPaid: 800, principalDue: 2650, principalPaid: 160, interestDue: 2400, interestPaid: 1150, penaltyDue: 3600, penaltyPaid: 1300,
          subRows: [
              { id: 'DEF123', code: 'HL', accountNo: '480001', debtDate: '2022-03-04T09:51:14.899Z', debtAmt: 2000, totalPaid: '', principalDue: 2000, principalPaid: 10, interestDue: 1000, interestPaid: 50, penaltyDue: 2000, penaltyPaid: 100 },
              { id: 'DEF123', code: 'DL', accountNo: '480002', debtDate: '2022-03-04T09:51:14.899Z', debtAmt: 3000, totalPaid: '', principalDue: 500, principalPaid: 20, interestDue: 500, interestPaid: 100, penaltyDue: 1000, penaltyPaid: 150 },
              { id: 'DEF123', code: 'DL', accountNo: '480003', debtDate: '2022-03-04T09:51:14.899Z', debtAmt: 4000, totalPaid: 100, principalDue: 100, principalPaid: 30, interestDue: 200, interestPaid: 200, penaltyDue: 500, penaltyPaid: 250 },
              { id: 'DEF123', code: 'DL', accountNo: '480004', debtDate: '2022-03-04T09:51:14.899Z', debtAmt: 5000, totalPaid: 200, principalDue: '', principalPaid: 40, interestDue: 300, interestPaid: 300, penaltyDue: '', penaltyPaid: 300 },
              { id: 'DEF123', code: 'AL', accountNo: '480005', debtDate: '2022-03-04T10:52:18.161Z', debtAmt: 6000, totalPaid: 500, principalDue: 50, principalPaid: 60, interestDue: 400, interestPaid: 500, penaltyDue: 100, penaltyPaid: 500 }
          ]
      },
  
      {
          id: 'XYZ001', code: ['PL', 'DL'], accountNo: ['878210', '878211'], debtAmt: 1200, totalPaid: 1500, principalDue: 1500, principalPaid: 300, interestDue: 1200, interestPaid: 500, penaltyDue: 200, penaltyPaid: 100,
          subRows: [
              { id: 'XYZ001', code: 'PL', accountNo: '878210', debtDate: '2021-09-21T11:38:53.733Z', debtAmt: 500, totalPaid: 600, principalDue: 800, principalPaid: 200, interestDue: 900, interestPaid: 500, penaltyDue: 200, penaltyPaid: 100 },
              { id: 'XYZ001', code: 'DL', accountNo: '878211', debtDate: '2021-09-21T11:38:53.733Z', debtAmt: 700, totalPaid: 900, principalDue: 700, principalPaid: 100, interestDue: 300, interestPaid: '', penaltyDue: '', penaltyPaid: '' },
          ]
      },
  ]
  
  const res = Object.values(givenData.reduce((acc, { id, code, accountNo, debtDate, debtAmt, totalPaid, principalDue, principalPaid, interestDue, interestPaid, penaltyDue, penaltyPaid }) =&gt; {
      acc[id] = acc[id] || { id, debtAmt, totalPaid, principalDue, principalPaid, interestDue, interestPaid, penaltyDue, penaltyPaid, subRows: [] };
      acc[id].subRows.push({ id, code, accountNo, debtDate, debtAmt, totalPaid, principalDue, principalPaid, interestDue, interestPaid, penaltyDue, penaltyPaid });
      return acc;
  }, {}));
  
  console.log(res)</code></pre>
  </div>
  </div>
  </p>