Vladimir Lukyanov
Vladimir Lukyanov

Reputation: 374

Javascript boolean array operations

I have an instrestion question about boolean logiс in javascript.

I have a FOR circle and created a trigger event, which will store TRUE or FALSE for IF statement:

var trigger = [];

for (i = 0; i < 3; i++ ){

    //Checking for empty fields
    if(this.form.rows[i].fields[0].input.val() === '') {
        if($(this.form.rows[i].row[0]).length){

        bla bla bla

        trigger.push(true)
        }

        else {

        trigger.push(false);
        }
    }

So in the end I want to check each statement of array for boolean operator AND

if(trigger & ??? )

Any help friends ?

Upvotes: 0

Views: 3561

Answers (2)

Manishearth
Manishearth

Reputation: 16188

(I've marked new lines of code with //@@@) I assume you want to check if each statement is true. Then, there are two ways, depending on if the "bla bla bla" is actually there.

If the forloop is only for checking if every field is empty (i.e. the "bla bla" is empty), and the trigger array isn't being used afterwards, your if statement can be compactly written as:

var trigger = [];
var empty=false; //@@@
for (i = 0; i < 3; i++ ){

    //Checking for empty fields
    if(this.form.rows[i].fields[0].input.val() === '') {
        if($(this.form.rows[i].row[0]).length){
        }

        else {
        empty=true; //@@@
        break; //@@@
        }
    }
}

If you want to be able to use trigger afterwards, do this:

var trigger = [];
var dummy=[];  //@@@@
for (i = 0; i < 3; i++ ){

    //Checking for empty fields
    if(this.form.rows[i].fields[0].input.val() === '') {
        if($(this.form.rows[i].row[0]).length){

        bla bla bla

        trigger.push(true)
        }

        else {

        trigger.push(false);
        }

        dummy.push(true);   //@@@@
    }

//Use this if block:
if(dummy.toString()=trigger.toString()){
   //Insert stuff here
}
//Alternatively,  use this:

if(dummy && trigger){
   //Insert stuff here
}

Upvotes: 1

KooiInc
KooiInc

Reputation: 122906

Based on one of your comments

Ok, how i can check each statement in array if it's TRUE, than return true

Like this, for example:

var alltrue = !/false/i.test(trigger.join(''));

Upvotes: 5

Related Questions