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

169
Views
Finding a specific object in state, removing it and replacing it in React

My current code finds an object in a state array, removes it and then adds a newValue in its place. I know using .splice() mutates the state directly which is not a recommended practice in React. What is the proper way to do what I am trying to do below?

      const found = clientServiceReferral.selectedTimeSlots.find(element => element.timeSlot === timeWithDate);

      if (found) {
        clientServiceReferral.selectedTimeSlots.splice(found, 1)
      }


      const newValue = {
        timeSlot: timeWithDate,
        value: value.value
      }

      setClientServiceReferral({
        ...clientServiceReferral,
        selectedTimeSlots: [...clientServiceReferral.selectedTimeSlots, newValue]
      })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would use map method, which returns a new array, to find and replace:

const updatedTimeSlots = clientServiceReferral.selectedTimeSlots.map((element) => {
    if(element.timeSlot === timeWithData){
      return {timeSlot: timeWithDate, value: value.value}
    } else {
      return element
    }
})
setClientServiceReferral({
    ...clientServiceReferral,
    selectedTimeSlots: updatedTimeSlots
})
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!