Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

71
Views
Combine duplicate elements and sum the amount

I have an array object with incomes, for any duplicate incomes I want to have a single record which has a sum of all duplicates.

This the array object

const incomes = [
  {
    incomeType: 'DIVIDEND',
    incomeSubType: 'RECEIPT',
    payerName: 'Employer',
    expiryDate: '2022-03-10',
    regularAmountCalculated: {
      value: 2500,
      currencyCode: 'AUD'
    },
    irregularAmountCalculated: null,
    bonusCalculated: {
      value: 200,
      currencyCode: 'AUD'
    }
  },
  {
    incomeType: 'DIVIDEND',
    incomeSubType: 'RECEIPT',
    payerName: 'Employer',
    expiryDate: '2022-03-10',
    regularAmountCalculated: {
      value: 2500,
      currencyCode: 'AUD'
    },
    irregularAmountCalculated: null,
    bonusCalculated: {
      value: 200,
      currencyCode: 'AUD'
    }
  }
];

I am expecting the output to have a only on record for Dividend and calculated amount as 5000.

Expected output

const incomes = [
  {
    incomeType: 'DIVIDEND',
    incomeSubType: 'RECEIPT',
    payerName: 'Employer 1',
    expiryDate: '2022-03-10',
    regularAmountCalculated: {
      value: 5000,
      currencyCode: 'AUD'
    },
    irregularAmountCalculated: null,
    bonusCalculated: {
      value: 200,
      currencyCode: 'AUD'
    }
  },
];

I have written:

const totals:any = [];
statementOfIncomes.forEach(x => {
  const obj = totals.find(o => o.incomeType === 'DIVIDEND');
  if (obj) {
    obj.regularAmountCalculated.value = obj.regularAmountCalculated.value + x.regularAmountCalculated?.value;
  } else {
    totals.push(x);
  }
});

The error I get is

ERROR TypeError: Cannot assign to read only property 'value' of object '[object Object]'

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would suggest using a Map to store all unique incomes. You have to loop over your incomes, check if the map already has an entry based on the incomeType. If not, add it to the map, else get the existing value and add the regularAmount to it. Last you have to convert your map back to an Array. This has a runtime of O(2n) instead of O(n^2) as stated in your first approach.

const map = new Map();

incomes.forEach((income) => {
  if (!map.has(income.incomeType)) {
    map.set(income.incomeType, income);
  } else {
    const existingIncome = map.get(income.incomeType);

    existingIncome.regularAmountCalculated.value += income.regularAmountCalculated.value;

    map.set(income.incomeType, existingIncome);
  }
});

const uniqueIncomes = Array.from(map, ([_key, value]) => value);

// [
//   {
//     incomeType: 'DIVIDEND',
//     incomeSubType: 'RECEIPT',
//     payerName: 'Employer',
//     expiryDate: '2022-03-10',
//     regularAmountCalculated: { value: 5000, currencyCode: 'AUD' },
//     irregularAmountCalculated: null,
//     bonusCalculated: { value: 200, currencyCode: 'AUD' },
//   },
// ];

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!