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

246
Views
How to convert if else shorthand to normal if else case

Below code, I update the react hook with if-else shorthand condition. To understand exactly this, I try to write it in normal if-else condition, but I couldn't. What is the equal case of this shorthand conditions?

This is working code.

const [array, setArray] = useState([]);

function addProduct(product){
  const ProductExist = array.find((item) => item.id === product.id);
  setArray(
   array.map((item) =>
   item.id === product.id
   ? { ...ProductExist, quantity: ProductExist.quantity + 1 }
   : item
   )
 );
}

This is my try according to the solutions of other topics. But it gives error.

setTestArray(array.map((item) => {
  return if(item.id === product.id){
   return { ...ProductExist, quantity: ProductExist.quantity + 1 }
  }else{
   return item
  }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

  setArray(
   array.map((item) =>
     if(item.id === product.id){
       return { ...ProductExist, quantity: ProductExist.quantity + 1 };
     } else {
       return item;
     }
   )

about 4 years ago · Juan Pablo Isaza Report

0

You have an extra return

It should be like this:


setTestArray(array.map((item) => {
  if(item.id === product.id){
   return { ...ProductExist, quantity: ProductExist.quantity + 1 }
  }else{
   return item
  }
}))

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!