Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
79
rated 0 times [  80] [ 1]  / answers: 1 / hits: 123885  / 6 Years ago, sat, december 22, 2018, 12:00:00

I have an array of booleans, which begins as false, because at least one of the values is false:
var validation = [false, true, true]
In certain moment, it'll have all the options(index) as true, like:
validation = [true, true, true]
How can I set this array as true when all the options are true?



Sorry for the silly question.


More From » arrays

 Answers
18

You can use .every() method:





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

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

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





As mentioned by @Pointy, you can simply pass Boolean as callback to every():





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

let checker = arr => arr.every(Boolean);

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




[#52882] Sunday, December 16, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jackie

Total Points: 442
Total Questions: 107
Total Answers: 94

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
jackie questions
Sat, Sep 18, 21, 00:00, 3 Years ago
Wed, Jul 14, 21, 00:00, 3 Years ago
;