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

125
Visualizações
if array contains duplicate, merge both values as one

I've got an array that looks like:

enter image description here

Is there a way that when another object "AAPL" gets added to the array, the fees and amount gets added and AAPL stays one object in the array?

Thank you in advance!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

First identify the entry then just add another integer to the value.

It depends on how you are updating that object now - if it's always in 0 position then just portfolioHoldings[0][amount] += number;

if not, then loop through all and check if ticker_symbol == AAPL, then update that array value, by adding needed amount.

about 4 years ago · Juan Pablo Isaza Relatório

0

I wouldn't want to assume that "AAPL" is always within the array.

Check first if the new item is within the array. If not, I will return and end the call (but you may wish to append this to the array instead as it is a new item).

If it is, replace the current object (or parts of the object, in this case I have done just fees & amount)

function addPortfolioItem(item) {
    const index = returnPortfolioHoldingIndex(item);
    if (index = -1) {
        // Returning if not found - You may wish to append to the array as mentioned above
        return;
    } else {
        // Overwrite the object within the array with the new object
        portfolioHoldings[index].fees = item.fees;
        portfolioHoldings[index].amount = item.amount;
    }
}

function returnPortfolioHoldingIndex() {
    const tickerSymbolToSearch = "AAPL";
    return portfolioHoldings.findIndex(item => item.ticker_symbol === tickerSymbolToSearch);
}
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