Answers for "find and update the value inside the object js"

0

find and update the value inside the object js

Step1: Inside state
state = {
  todos: [
   {
     id: 1, 
    title: "Eat breakfast",
    completed: false
   },
   {
     id: 2,
    title: "Make bed",
    completed: false
   }
  ]
}

Steps: Inside function where you need to update the value
const elementsIndex = this.state.todos.findIndex(element => element.id == id );

let newArray = [...this.state.todos]

newArray[elementsIndex] = {...newArray[elementsIndex], completed: !newArray[elementsIndex].completed}

this.setState({ todos: newArray });
Posted by: Guest on September-07-2021

Code answers related to "find and update the value inside the object js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language