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

110
Views
I'm having trouble removing an item from localStorage for my tasklist app which i'm building using vanilla javascript

Here's a function i'm having trouble with the foreach loop traverses just fine but i'm having trouble removing item from localStorage.

function removeFromLocalStorage(taskItem) {
  let tasks;
   if (localStorage.getItem("tasks") === null) {
      tasks = [];
   } else {
      tasks = JSON.parse(localStorage.getItem("tasks"));
   }

   tasks.forEach((task, index) => {
      if (task === taskItem) {
      tasks = localStorage.removeItem(task);
      }
  });
  }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

try this

function removeFromLocalStorage(taskItem) {
     let tasks;
     if (localStorage.getItem("tasks") === null) {
        tasks = [];
     } else {
        tasks = JSON.parse(localStorage.getItem("tasks"));
     }

     let result = tasks.filter(task => task != taskItem);
     localStorage.setItem('tasks', JSON.stringify(result));
  }
about 4 years ago · Juan Pablo Isaza Report

0

Looks like you are trying to remove an element in the array that you have stored in localstroage.

You have to remove the element from the array and then set it back

function removeFromLocalStorage(taskItem) {
    // gets the array from the localstorage
    let tasks = JSON.parse(localStorage.getItem("tasks")) || [];
    // removing the taskItem by filter method and setting it back
    localStorage.setItem("tasks", tasks.filter(task => task !== taskItem));
}
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!