1

I have a bool array.

var arr = [true, false, true,false, true]

My requirement:

If the array contains a bool value true I want to show a single alert 'array contains a true value'. Alert should not be multiple. Can someone suggest how to achieve it in javascript?

D-Shih
  • 44,943
  • 6
  • 31
  • 51
Soumya Behera
  • 2,325
  • 4
  • 15
  • 25
  • 2
    Possible duplicate of [How do I check if an array includes an object in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) – Rajesh Feb 05 '18 at 05:07
  • Soumya, Please note that SO is **not get code for free** site. You have to try first and if you end up with some problem, share the problem with your attempt and we will help you. – Rajesh Feb 05 '18 at 05:14

4 Answers4

5

includes will do

var arr = [true, false, true,false, true]
if(arr.includes(true)){
alert("true found");
}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
4

you can use Array.prototype.some for this purpose also.

var arr = [true, false, true,false, true]
if(arr.some((elem)=> elem === true))
{
 console.log('contains true')
}

You can also use Array.prototype.findIndex method. If not found it will return -1.

if(arr.findIndex(elem=>elem === true)!=-1){
    console.log('contains true')
}

Object.is ( ) uses === internally. So you can use it as well

if(arr.some(elem=>Object.is(elem,true))){
   console.log('contains true')
}

array.prototype.indexOf also uses === internally.

if(arr.indexOf(true) != -1){
   console.log('contains true')
}

There are so many ways to choose from.Pick the one that suits your need.

AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • First, you should use `===` instead of `==`. Second, you can use `arr.some(x => x === true)`. No need to manually create a block and return if there is only 1 line. Third, its not a good practice to have looping mechanism as condition in `if`. Better save it in variable and use it. – Rajesh Feb 05 '18 at 05:12
  • you can use == because you are comparing things with the same type, also you can use arr.some(x => x), and the condition inside the if its evaluated before the if – Sebastián Espinosa Feb 05 '18 at 05:17
  • @SebastiánEspinosa `==` tries to convert to a string and compare but `true == 'true'` is false so adding `==` is, in my POV, wrong. Using `x=>x` is also not idea as OP explicitly wants to check for `true` and not thuthy value. Third, *condition inside the if its evaluated before the if* its obvious. But the point is it falls under bad conventions, at least for me. There is a level below which brevity results in loss of readability. You should maintain a balance. Code should be short but also be readable. – Rajesh Feb 05 '18 at 05:23
  • 1
    @Rajesh thanks for pointing this out. I should've used strict equality operator in the first place – AL-zami Feb 05 '18 at 05:30
0

You could try this:

for(var i=0; i<arr.length; i++){
    if(arr[i]){
        alert("Array contains a true value");
        break;
    }
}

OR

var b = false;
for(var i=0; i<arr.length; i++)
    b = b || arr[i];

if(b) 
    alert("Array contains a true value");
Iman Norouzi
  • 11
  • 1
  • 6
  • there are plenty of convenient built in methods in javascript that can serve this purpose well instead of using plain for loop. For loop shouldn't be used so frequently in javascript code as it reduces readability. – AL-zami Feb 05 '18 at 05:45
0

You could simply use Array.prototype.some, Following is the code.

let arr = [true, false, true,false, true]
if(arr.some(e=>e))
  alert("true is included in the array");
Skyler
  • 656
  • 5
  • 14
  • Did you check other answers if this approach is covered or not? – Rajesh Feb 05 '18 at 05:15
  • It was covered. But this code is more concise. Therefore thought of adding. – Skyler Feb 05 '18 at 05:18
  • Please refer my comments under that answer. Also, if there is a concise way for someone's answer, its always better to comment it under it. Adding another answer is duplicating the content. – Rajesh Feb 05 '18 at 05:24