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

270
Views
There is a bug in my array's adding/updating function. May I know why is that?

I have a array of object.

The structure of object is as below, having two keys: one is name, the other is a

{name:'set1',a:1}

Below is my programming to handle an add object function into it;

let totalData = [] 

console.log(totalData)

handleUpdateTotalData = (updateJson) => {
  let duplicate = false;
  tmpTotalData = TotalData.map((element,index) => {
    if( element.name === updateJson.name ){
      duplicate = true
      return updateJson
    }
    return element
  })
  return duplicate? tmpTotalData: (totalData = [...totalData,updateJson])

}

handleUpdateTotalData({name:'set1',a:1})

console.log(totalData)

handleUpdateTotalData({name:'set1', a:2}) //since probety name:'set1' has occured, so it should update the previous object instead of adding a new one

console.log(totalData)
//expected to show {name:'set1', a:2}
//But now it show{name:'set1', a:1}

Q1: In my program, the expected outcome is not as my expected, May I ask why is that?

about 4 years ago · Juan Pablo Isaza
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!