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

179
Views
I have to edit only one property in a JS object, can I spread only a portion of a said object when passing it as an argument to a function?

I have a JavaScript object with the following structure:

{
    id: 0,
    content: "English Class",
    date: "Dec 24th at 7:30pm",
    completed: false,
}

I also have a function const editTask(id, content) that updates the content property in the aforementioned object:

const editTask = (id, content) => {
    setTasks(tasks.map((task) =>
      task.id === id ? { id: task.id, content: content, date: task.date, completed: task.completed } : task));
  }

Is there a way in which I can avoid passing all the object properties to only upgrade the content property as well as just spreading the rest of the object?

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

0

A common pattern is to combine the spread syntax with manually setting some properties:

const editTask = (id, content) => {
    setTasks(tasks.map((task) =>
      task.id === id ? { ...task, content } : task));
  }

In the above example, the spread syntax sets all the properties of task, and the next line overrides the property content to the new value. It's important that the spread is before the content. If it were after, then the new value would be overridden by the spread.

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!