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

227
Views
Add unique object into array reactjs

i have problem here, when i click into one element 2 times it will add 2 time into my array, i dont know how to add unique element into array, can u guys help me? Thank you guys

 const handleClick = (
    id: number,
    name: string | null,
    avatar: string | null
  ) => {
    setListChoose((prevState: any) =>
      [...prevState].concat({ id, name, avatar })
    );
  };

enter image description here

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

0

You can check that whether that contact exists in array or not. If not then append in list else don't.

const handleClick = (
    id: number,
    name: string | null,
    avatar: string | null
  ) => {
    let list = Array.from(listChoose); //assuming that list name is listChoose.
    if(list.find(l => l.id === id) === undefined){
       setListChoose((prevState: any) =>
          [...prevState].concat({ id, name, avatar })
       );
    }
  };
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!