Logic of if with type bool and type object

-1

Hello, I'm breaking my head on this logic and I can not solve it;

I have this code:

for(let i = 0; i < mensagemDeErroDoInput.length; i++) 
{
    if(statusInput[mensagemDeErroDoInput[i].nome] === true)
    {
        return mensagemDeErroDoInput[i].mensagem;
    }
}

An IF within a FOR, very simple!

Imagine this:    I have two types of parameters, one parameter returns bool and the other returns me an object;

return parameter bool, returns this: true;

return parameter object, return this: {valid: false};

These parameters are my error message references, that is, if my return parameter bool is true, then I return an error message.

And if my object return parameter is other than null, then I return an error message.

However, all my bool return parameters are automatically different from null. So I'm not able to do this validation inside my for.

Could someone please help me?

Thank you in advance ...

    
asked by anonymous 06.06.2018 / 23:36

3 answers

1

Tests the desired type and value as follows:

for(let i = 0; i < mensagemDeErroDoInput.length; i++) 
{
    var statusInputCorrente = statusInput[mensagemDeErroDoInput[i].nome];
    if((typeof statusInputCorrente === "object" && statusInputCorrente != null) || (typeof statusInputCorrente === "boolean" && statusInputCorrente === true))
    {
        return mensagemDeErroDoInput[i].mensagem;
    }
}
    
07.06.2018 / 00:42
1

I think what you want would be this:

for (let i = 0; i < mensagemDeErroDoInput.length; i++) {
    var erro = mensagemDeErroDoInput[i];
    var status = statusInput[erro.nome];
    if (typeof status === "boolean" ? status === true : status !== null) {
        return erro.mensagem;
    }
}
    
07.06.2018 / 00:17
1

An object will always be true, so if you want to see if it exists use the if (typeof object_name == "undefined") {code} And with Bolean question I think you'd better take a look at this one link

The first and second answers can solve your problem.

    
07.06.2018 / 00:30