-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?
Possible duplicate of How to check if at least one item of the array has value equal to or greater than 2
– Woss
The Every() method tests whether all array elements pass the test implemented by the given function. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
– user60252