Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a boolean true if all the values in an array are true (strings) and if one of the value is false(string) stop checking using Javascript

I have an array

var myarr = ["true","false","true"];

I want the above to return false of boolean type.

var myarr = ["true","true","true"];

I want the above to return true of boolean type. Can someone please help with what's the most efficient way to achieve this?.

Is it best to first convert the string to boolean in another array and then use .every()?.

Please advise. Thank You.

like image 742
curiousCat Avatar asked Dec 08 '25 14:12

curiousCat


2 Answers

Check your answer out here, you'll certainly get a more in-depth answer here: Check if all values in array are true, then return a true boolean statement (javascript)

let arr1 = ['true', 'false', 'true'],
    arr2 = ['true', 'true', 'true'];

let checker = arr => arr.every(v => v === 'true');

console.log(checker(arr1));
console.log(checker(arr2));

Check that .every value is 'true'.

return myarr.every(val => val === 'true');

like image 37
CertainPerformance Avatar answered Dec 10 '25 04:12

CertainPerformance



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!