2

Associative array:

var signUpStates = {
    "CNPJ": false,
    "PASSWORDS": false,
    "ADDRESS": false,
    "GENERAL_FIELDS": false,
    "TERMS": false,
}

My attempt:

function updateButton() {
    var tempArray = []
    $.each(signUpStates, function(i, val) {
        tempArray.push(val);
    });
    if(tempArray.every(function(e, i, a) { return e == true; })) {
        $(".btn-cadastrar-fornecedor").attr('disabled', false);
    } else {
        $(".btn-cadastrar-fornecedor").attr('disabled', true);
    }
}

Iterating over it and checking individually does not work. What's the best approach to do such a test?

3
  • there should be a .all() function, depending on which lib you are using Commented Nov 25, 2015 at 18:25
  • 2
    In JavaScript, we just call them objects. Commented Nov 25, 2015 at 18:29
  • You should consider stating your array objects by setting its position like signUpStates = [], cnpj[0] = false, passwords[1] = false. Commented Nov 25, 2015 at 18:31

2 Answers 2

4

The most straighforward solution (if you are asking about logic) would be to use for loop:

var is = true;

for (var key in signUpStates) {
    if (!signUpStates[key]) {
        is = false;
        break;
    }
}

Strictly saying, correct approach would be also to check only own properties, this is important if the object you are testing might inherit properties from another objects:

var is = true;

for (var key in signUpStates) {
    if (signUpStates.hasOwnProperty(key) && !signUpStates[key]) {
        is = false;
        break;
    }
}

In simple cases like yours, this additional check is not needed.

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

2 Comments

May be worth a mention of hasOwnProperty. Not necessary given the OP's code, though. And +1, sometimes the old ways are best.
Yes, I thought about hasOwnProperty but in OP's case it's not necessary. But yes, better to mention.
1

You can get an array of the object's property names from Object.keys, and then use Array#every:

if (Object.keys(signUpStates).every(function(name) { return signUpStates[name]; })) {
    // All are true
} else {
    // Not all are true
}

With ES2015's (aka ES6) arrow functions, this becomes a bit shorter:

if (Object.keys(signUpStates).every(name => signUpStates[name])) {
    // All are true
} else {
    // Not all are true
}

Support for ES2015 is still relatively thin on the ground, particularly if you have to handle browsers that were released before the spec was finalized, so if this is for a general web page, you'd not use them or use a transpiler. If this is for, say, NodeJS, you can use them directly in v4 and higher.

1 Comment

@ZohaibIjaz: Far from it, I assume (but probably shouldn't) that people know that. I use transpilation.

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.