1

I have framed an array like below

iArray = [true, true, false, false, false, false, false, false, true, true, true, false, 
true, false, false, false, false, true] 

Condtional check: If anyone of the value in this array is false I will be showing an error message else if everything is true I will be showing success message.

I tired below code to iterate, however couldn't frame the logic in it.

var boolIteration = iArray.split(',');
var i;
for (i = 0; i < boolIteration.length; ++i) {
    //conditional check
}

I'm struggling to iterate the array using the above condition.

Can anyone point me in the right direction with an efficient solution.

5
  • 2
    iArray is already an array, why are you trying to split it like a string? Commented Sep 14, 2013 at 11:48
  • @Kolink inorder to iterate the values in it. Is that wrong? Commented Sep 14, 2013 at 11:51
  • Erm, yeah! .split is to convert a string into an array by a separator! Commented Sep 14, 2013 at 11:52
  • 1
    if(~$.inArray(false, iArray)){...} Commented Sep 14, 2013 at 11:58
  • @A.Wolff Though I wanted to do this JS. Since you have mentioned that indexOf has compatibility issue in IE, I will go with your jQuery solution. Thank you. Commented Sep 14, 2013 at 12:15

5 Answers 5

4

No need for jQuery

if (iArray.indexOf(false) !== -1) {
    // error
}

Also, as previous commenters have already pointed out, iArray is already an array, there's no need to use split on it.

The Array.prototype.indexOf is not available in Internet Explorer below 9. However, this functionality could be easily added with a matching algorithm. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf for compatibility and how to create a workaround.

Sign up to request clarification or add additional context in comments.

4 Comments

the only problem here is the lack of indexOf method for prototype array on older IE versions
@A.Wolff You are absolutely right. Below IE9 indexOf is by default a no-no, but could be shimmed.
… and a shim is still better than loading jQuery for something as simple as this. jQuery solutions are only preferable if jQuery is already being used anyway.
+1 Thank you for showing me the approach in JS. Since I already using jQuery I will go with the above one.
2

iArray is already an array, so there is no need to split it again (split is a method for String, Arrays don't have it)

What you need to do is check the index of a false value, if it is there then there is a false value in the array.

using jQuery - the array indexOf is not used because of IE compatibility

iArray = [true, true, false, false, false, false, false, false, true, true, true, false, 
true, false, false, false, false, true] 
if($.inArray(false, iArray ) != -1){
    //error
}

1 Comment

+1 I thought it would be indexOf, but jQuery has a pretty simple method to handle this. Thank you for your pointers.
1
 var iArray = [true, true, false, false, false, false, false, false, true, true, 
true, false,true, false, false, false, false, true];

    for (var i = 0; i < iArray.length; i++) {
        if (iArray[i]) {
            alert("success");
        }
        else {
            alert("error");
        }
    }

13 Comments

i is less than an array? That's a new one.
Yes. If size of array is 10, then index of his last element will be 9. First element always has 0 as index.
@Kolink You're just full of constructive criticism, aren't you?
@AlexanderPerechnev He's trying to be funny and/or clever. What he should have said is that you should have put for (var i = 0; i < iArray.length; i++)
@IngoBürk It doesn't work like that here. People will post almost anything just to get their answer in first, and then edit it to something (hopefully) correct later. It's called being point-hungry and this site is full of it. sadpanda.jpg
|
1

Aternatives:

if (/false/i.test(iArray)) { }

or

if ( ''.replace.call(iArray,/true|,/g,'').length ) { }

8 Comments

.sort modifies the original array, it does NOT return the sorted array.
Besides, sort has a worse performance than indexOf. Even iterating over the array would still be better, if you don't want to use indexOf.
The new alternatives seem unnecessarily cryptic and complicated. Sure one can understand what they do, but not without having a look at it. What's the point of such solutions? When are they ever better than the "normal" solution(s)?
You are entering the geek zone, beware :3
If you just want "weird" solutions, here's another one: iArray.reduce(function (a,b) { return a && b; });.
|
0

The jQuery.inArray function is nice for checking to see if a particular value is contained within an array.

iArray = [true, true, false, false, false, false, false, false, true, true, true, false, true, false, false, false, false, true];
if ($.inArray(iArray, false) >= 0) {
  // the value "false" is contained within the array, show an error message
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.