4
I have some fields where I pass some filters for a query in the bank. I would like to check if the object is empty, so the query would not be held.
I tried that way:
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
Step the object this way:
$scope.filtrarContratos = function(contrato) {
if(isEmpty(contrato)){
console.log("Inválido");
}
}
But the result is always false
.
This is the result I get on the console when I do a console.log of the object:
Object { contrato: "" }
I tried to do an if to check if it was equal to ""
but it didn’t work either.
Have you tested these examples? http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
– Samir Braga
@Samirbraga was right to share the link.
– durtto
@Samirbraga yes I had tried and had not worked, but I think I solved the problem. Instead of sending the whole object to the function
isEmpty
I tried, I sent attribute by attribute and it worked.– DiegoAugusto
Ever tried to make
!!obj
?– Marllon Nasser
It also has: Function isEmpty(obj){ Return (Object.getOwnPropertyNames(obj).length === 0); }
– durtto
@durtto had also done this test. Now it’s working the way I said, I think the object was not being created correctly. because this contract that appears above is an attribute too
– DiegoAugusto
If everything went well, post the solution as an answer, can help someone else later.
– Samir Braga
various forms exist, but you need to make clear the syntax of your object. Stay tuned to the prototype
– durtto