Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

78
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda