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

140
Views
Confusion regarding Spread Operator in JS

So basically I have list of objects. I am iterating and have to change one property of the object. I have two scenarios, in one I have to change the property of a particular object.

 setList(list.map((list_item)=>
      list_item.id===id?{...list_item,doubleClick:true}:list_item
    )

And there is another scenario where I have to change the property of every object in the list.

setList(list.map((list_item)=>{...list_item,doubleClick:false}))

The first case is working fine but the second case is throwing error >Parsing error: Unexpected token (34:35)

To me they don't look that different except first one has ternary operator? Is that what's making a difference? How to fix the second case?

Edit: So I think spread operator works with an expression or declaration.

setList(list.map((list_item)=>list_item={...list_item,doubleClick:false}))

This works. Is there any better or correct way to do it?

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

0

In your second case, you need to return the new object.

setList(
  list.map((list_item) => {
    return { ...list_item, doubleClick: false };
  })
);

Or a shorter way is to wrap the new object inside ()

setList(list.map((list_item) => ({ ...list_item, doubleClick: false })));

Read more about the syntax here

Edit:

The syntax in the third case is fine but the logic isn't correct. You want to return the new object, not reassign elements in .map()

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!