I have been trying to make the code below work, which is a part of deletion function. However, the splice method inside the for-loop keeps returning an error when it's run.
Can anyone please give me a solution to this problem?
let games = JSON.parse(localStorage.getItem('games'));
for (let i =0; i < games.length; i++) {
if (games[i].gName == gName) {
console.log('found it');
games[i].splice(i, 1);
//Uncaught TypeError: games[i].splice is not a function
}
}
I think the splice function returns an error because games[i]
is an object, not an array.
If you want to use splice on the array, just remove the [i]
part.
games.splice(i, 1);
Also you can use for (i in games)
as a shorthand for for (let i =0; i < games.length; i++);