Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda