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

221
Visualizações
Why won't my stringify parse in an "if" statement?
const friends = ["john","peter","bob"];
localStorage.setItem("friends", JSON.stringify(friends));

const values = JSON.parse(localStorage.getItem("friends"));
console.log(values[2]);
// console.log(values[1]);
// console.log(values[2]);

let fruits;
console.log(fruits);

if (localStorage.getItem('fruits')) {
 fruits = JSON.parse(localStorage.getItem('fruits'));
}
else {
 fruits = [];
}

console.log(fruits);
fruits.push("apple");
fruits.push("orange");
localStorage.setItem("fruits", JSON.stringify(fruits));

Why will the first array behave properly in chrome where I can clearly check the console and application from dev.tools but when I try to make an empty array and push different values fx "apple" and "orange" I can't. My console says the error is in the "if" statement, but I don't understand why. Any thoughts?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I believe the answer lies in the quotes. JSON syntax is very strict and requires the use of double quotes. Change out all your single quotes in your if statement to double quotes and see if that fixes your issue.

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