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

123
Views
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 answers
Answer question

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 Report

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 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!