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

159
Views
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 answers
Answer question

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 Report

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 Report

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 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!