Check empty object Angularjs

Asked

Viewed 4,406 times

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.

  • 1

    Have you tested these examples? http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object

  • @Samirbraga was right to share the link.

  • @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.

  • Ever tried to make !!obj?

  • It also has: Function isEmpty(obj){ Return (Object.getOwnPropertyNames(obj).length === 0); }

  • @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

  • 1

    If everything went well, post the solution as an answer, can help someone else later.

  • 1

    various forms exist, but you need to make clear the syntax of your object. Stay tuned to the prototype

Show 3 more comments

3 answers

6

Corrected function, here worked

function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop) && obj.prop != "" && obj.prop != undefined)
           return false;
    }
    return true;
}
  • That way it didn’t work either.

  • I simulated the object that was passed in question Object { contrato: "" }, and it worked

  • See how I answered: http://jsfiddle.net/sinkz/HB7LU/21629/

  • And here you are implementing your solution: http://jsfiddle.net/sinkz/HB7LU/21630/

  • I think I agree with you, because if contrato.contrato is filled the other fields not the object is not empty, only incomplete. It makes sense to create a function isCompleteObject make sure the object is complete. I guess I missed specify this in the question.

  • I don’t really understand how JS works, but I kind of noticed this, because if I don’t fill in the other fields these attributes don’t even appear in the object. I guess it’s because I’m not "creating" him with those attributes at the beginning

Show 1 more comment

3

The given Function as fix does not work properly, this is because it does (obj.prop != ""), and the right thing would be (obj[prop] != ""). The Function that checks if any of the items of the object is empty would be:

function isEmpty(obj) {
            for(var prop in obj) {
                if(obj[prop] != "")
                    return false;
            }       
            return true;
        };

The Function that uses

if(obj.hasOwnProperty(prop)), only works if you want to check if a particular item has been added to the object, but if the same item is there, and the idea is to check if it is empty, then this function you use hasOwnProperty no longer serves.

2


I solved the problem by checking the object attributes instead of the whole object.

Ex:

$scope.filtrarContratos = function(contrato) {
    if (isEmpty(contrato.contrato) && isEmpty(contrato.contratante) && isEmpty(contrato.codigoAgrupador)) {
        console.log("Inválido");
    }
}

Function that checks if it is empty:

function isEmpty(obj) {
    for(var prop in obj) {
      if(obj.hasOwnProperty(prop))
      return false;
    }

    return true;
  }

Html:

<div class="row">
   <div class="col-md-3">
      <input class="form-control" type="text" ng-model="contrato.contrato" placeholder="Contrato"/>
   </div>
   <div class="col-md-3">
      <input class="form-control" type="text" ng-model="contrato.contratante" placeholder="Contratante"/>
   </div>
   <div class="col-md-3">
      <input class="form-control" type="text" ng-model="contrato.codigoAgrupador" placeholder="Cod. Agrupador"/>
   </div>
   <div class="col-md-3">
      <button class="btn btn-primary btn-block" ng-click="filtrarContratos(contrato)">
      <i class="fa fa-search"></i>
      Filtrar
      </button>
   </div>
</div>

Working example: http://jsfiddle.net/sinkz/HB7LU/21629/

  • Then you need to change your question,

  • Change in what way?

  • Check if properties of an empty object are empty with Angularjs

  • I’ve already specified that in the title

  • Checking empty object or property of an empty object has a very big difference @Techies

  • @Otto my initial idea was to check the object because as I was unable to check the property of the empty object. I’m making a fiddle to show you :D

  • http://jsfiddle.net/sinkz/HB7LU/21629/

Show 2 more comments

Browser other questions tagged

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