I have an array of objects and I want to replace one of the objects in an array with a new object for which I have used an array method of indexOf and splice. But it is not working also in the log I found the value I am getting using indexOf is '-1', can anyone please help me.
my state
export const todos = [
{
id: "1",
item: "Buy Milk"
},
{
id: "2",
item: "Buy Apples"
},
{
id: "3",
item: "Buy Banana"
}
];
action.payload
[
{
id: "1"
item: "Buy Milk"
},
{
id: "1"
item: "Do exercise"
}
];
My code
case UPDATE_TODO:
newTodos = [...state];
console.log(action.payload[0]); //{id: "1",item: "Buy Milk"}
let getIndex = newTodos.indexOf(action.payload[0]);
console.log(getIndex); // getting -1 instead of 0
newTodos.splice(getIndex, 1, action.payload[1]);
return newTodos;