Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
How do I delete an item from An array stored on local storage-JavaScript

Am trying to implement a feature where an item is to be deleted from local storage when a user clicks a button. The item is part of an array stored in local storage. However am not able to here is how the item is saved

const favBtns=meal.querySelectorAll(".fav-btn i")
    const images = document.querySelectorAll('.meal-header > img')
    favBtns.forEach((favBtn,i)=>{
      favBtn.addEventListener("click", ()=>{
        favBtn.classList.toggle("active")
        console.log(images[i].src, images[i].alt)     
        myFavMeals.push({src: images[i].src, alt: images[i].alt})
        localStorage.setItem("myFavMeals", JSON.stringify(myFavMeals))
        showMeal
    })     
})

here is how am trying to delete the item

   function clearfav(){
        const clears=document.querySelectorAll("i#clear.fa.fa-window-close")
      clears.forEach(clear=>{
        clear.addEventListener("click",()=>{
          let favmeals= JSON.parse(localStorage.getItem("myFavMeals"))
          favmeals.forEach((favmeals,i)=>{
            favmeals.splice(favmeals[i],1)
            showMeal()
           })
        })   
      })
      }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. Don't redaclare same variable name: favmeals is your parsed array and also favmeals is array element inside of forEach. Rename one of them.
  2. Don't forget to set updated local storage variable after you changed it.
// With [1][2]
function clearfav() {
    const clears = document.querySelectorAll("i#clear.fa.fa-window-close")
    clears.forEach(clear => {
        clear.addEventListener("click", () => {
            let favmeals = JSON.parse(localStorage.getItem("myFavMeals"))
            favmeals.forEach((favmeal, i) => {
                favmeals.splice(favmeals[i], 1)
                showMeal()
            })
            localStorage.setItem("myFavMeals", JSON.stringify(favmeals))
        })
    })
}
  1. I guess you don't need forEach inside of "click" event handler at all, because now you trynna to remove each element. For example if you wanna remove only element with same index with icon you clicked on:
// With [1][2][3]
function clearfav() {
    const clears = document.querySelectorAll("i.fa.fa-window-close")
    clears.forEach((clear, index) => {
        clear.addEventListener("click", () => {
            let favmeals = JSON.parse(localStorage.getItem("myFavMeals"))
            favmeals.splice(index, 1)
            localStorage.setItem("myFavMeals", JSON.stringify(favmeals))
        })
    })
}

Also should be noticed: i#clear.fa.fa-window-close is wrong query, bacause it means you have multiple elements with same id. id should be unique, use classes instead.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!