I am trying to use the splice() to delete a specific item in an array stored in local storage. but when I click the delete button, all it does is delete the item from the page but not from local storage. How do I solve this?
let localStorageKey = 'userArray';
if (localStorage.getItem(localStorageKey) === null) {
userArray = [];
}
else {
userArray = JSON.parse(localStorage.getItem(localStorageKey));
}
userArray.forEach((name) => {
const para = document.querySelector('p');
const delBtn = document.querySelector('.delBtn');
let delTitem = userArray.splice(name, 1);
delBtn.addEventListener('click', () => {
para.textContent = '';
localStorage.removeItem(delTitem);
});
para.innerHTML = `${name.firstname} ${name.lastname}`;
.filter()to remove it. 2. the item is not removed from localStorage because you never call.setItemto update it anywhere.setItemfor it to be removed? It may have been set before..getItemreads the string stored there, it does not return a reference to an object you can change. In order to update the stored array, you have to stringify it and store it using.setItem