Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda