Foreach in Javascript

Asked

Viewed 1,617 times

-1

Hello, I have an object in JavaScript and would like to check if all the values of the object keys are different from null, undefined, 0, "" and " ".

I traverse each value of my object through the forEach, but I want the first condition to be executed only if all values are different from what I commented above.

In my code, if only one value is no longer null, undefined, 0, "" or " ", my code performs the condition and does not enter Else anymore.

Beautified JavaScript:

let dados = {
    nome: nome,
    email: email,
    telefone: telefone,
    cursos: cursos,
    cidade: cidade
};
let condicao = false;
Object.values(dados).forEach(function (campo) {
    console.log(campo);
    if (campo !== "" && campo !== null && campo !== " " && campo !== false && campo !== 0 && campo !== undefined) {
        //Execute algo
    } else {
        if (condicao == false) {
            alert("Preencha todos os campos");
            condicao = true;
        }
    }
})

Someone can help me?

2 answers

2

The object in JS is key-value.

You can use for...in, follow the documentation below

//Objeto
var obj = {a:1, b:2, c:3};

//Para prop (propriedade) in obj (objeto) faça
for (var prop in obj) {
  // ctrl+shift+k (para abrir o console no mozilla firefox)
  console.log("obj." + prop + " = " + obj[prop]);
}

//A saída (output) deverá ser:
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

  • 2

    I think that despite the question title, this answer does not clarify the AP. Look at the text of the question, and see if you can address his real doubt.

  • The for...in is not a foreach. It runs through the properties of the object. My answer was more of a north. In this case, he could create a function, use for...in and simply see if the prop was empty, null, zero or undefined, and if it was, return true.

0


out of a method, I imagine the resolution in a simple and verbose way as following:

let dados = { nome: '', email: 'email', telefone: 'telefone', cursos: 'cursos', cidade: 'cidade' };

let count = 0;

Object.values(dados).forEach(function (campo) {
    if (!campo || campo === ' ') {
        count++;
    }
});

if (count === 0) {
    console.log('todos preenchidos');
} else {
    console.log('falta preencher');
}

Just stick to a few details:

  • In javascript, 0, null, Undefined and blank are values that return false, so in a check if you use "! var", for any of these values that is in the variable, will return false;
  • You used "&&" for checking, in which case the value of a variable would have to be, according to your example, null, blank, Undefined, etc... and it is impossible for a value to have all these rsrs attributes, in which case you should use "||"therefore, if the value has any of these conditions, it will return false

I hope I helped, good luck.

  • Instead of forEach, research on the every or some that the solution will become simpler.

  • yes, yes, but from the level of the code, it looks like he’s new to the language, so I thought I’d put something more verbose as an example.

  • another detail is also never use "space" check because depending on the character it can be "invisible" but it is not "space", in which case you can use something like "field.length > 0"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.