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 });