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

187
Views
Remove object key from localStorage object variable without removing from localStorage

I need to remove object key from localStorage object variable without removing key from localStorage

let obj = localStorage;
let keys = Object.keys(localStorage).sort(function(a, b) {return a - b;});

for(let i = 0; i < keys.length; ++i){
    if (isNaN(keys[i])) {
        delete obj[keys[i]];
        keys.splice(i,1);
    }
}

code above removes non-numeric object key from obj variable, but also key is removing from localStorage

How to remove key from localStorage object variable and keep that key in localStorage?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

What you’re doing here is you’re copying the reference of the localStorage object and working on the localStorage itself What you can do is instead of getting localStorage like this:

const obj = localStorage;

Do like this:

const obj = {...localStorage};

So what you’re doing here you’re copying the object by value and not by reference

about 4 years ago · Juan Pablo Isaza Report

0

Problem solved by using Object.values(localStorage) and obj.slice(i,1) instead of "delete" now i can safe remove keys from obj variable without removing localStorage keys and can get obj.length too.

let obj = Object.values(localStorage),
keys   = Object.keys(localStorage).sort(function(a, b) {return a - b;});
for(let i = 0; i < keys.length; ++i){
    if (isNaN(keys[i])) {
        keys.splice(i,1);
        obj.splice(i,1);
    }
}
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!