I need to remove an object from my List array by matching its properties value with the event target ID. Currently, I am using findIndex method to locate the index ID that matches the event.target.id.
Below is an example of one of the objects in my list array:
{artist: "artist name",
genre: "RnB",
id: 1,
rating: 0,
title: "song name"}
This is the code snippet I'm working with:
console.log(e.target.id);
const list = this.state.playlist;
list.splice(
list.findIndex(function(i) {
return i.id === e.target.id;
}),
1
);
console.log(list);
}
However, instead of removing the clicked item from the array, it always removes the last item.
When I try this approach:
const foundIndex = list.findIndex((i) => i.id === e.target.id)
console.log(foundIndex)
The console logs -1 as the output.
What could possibly be causing this issue?