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

161
Visualizações
How to convert an array of strings to boolean using javascript?

i want to convert an array of strings to array of boolean using javascript.

i have an array of strings like below

const data = ["true", "false", "false", "false", "true"]

how can i convert above array of strings to array of booleans like below,

const data = [true, false, false, false, true]

how can i do this using javascript. could someone help me with this? thanks.

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

0

You could map the parsed values.

const
    data = ["true", "false", "false", "false", "true"],
    result = data.map(j => JSON.parse(j));
    
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

if you want to save the result into a new variable use map

const data = ["true", "false", "false", "false", "true"]
const result = data.map(e => e === 'true')

if you want change the original variable (the variable named "data") use forEach:

data.forEach((e, i) => { data[i] = e === "true" })
about 4 years ago · Juan Pablo Isaza Relatório

0

Assuming you want to change the existing array instead of generate a new one... Just check to see if each one is "true."

You could use JSON.parse as well, but that's probably overkill if you just have true and false, and it might throw an error if there's anything else in the array.

const data = ["true", "false", "false", "false", "true"];

for(var i=0; i<data.length; i++) data[i] = data[i] === 'true';

console.log(data);

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