0

Can you tell me how to check the JavaScript object has a value ? This vm.occupantDetail.contactDetail object is neither null nor undefined.It looks like as shown below at run time.

It defines as shown below.

 vm.occupantDetail = {
            contactDetail: {},
     };

enter image description here

Sampath
  • 63,341
  • 64
  • 307
  • 441
  • Possible duplicate of [How do I check if an object has a property in JavaScript?](http://stackoverflow.com/questions/135448/how-do-i-check-if-an-object-has-a-property-in-javascript) – U r s u s Jun 28 '16 at 10:58
  • Possible duplicate of [How do I test for an empty JavaScript object?](http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object) – James Thorpe Jun 28 '16 at 10:59
  • will you share your javascript object ? – M.Tanzil Jun 28 '16 at 10:59

4 Answers4

3

You can find the it using

Object.keys(vm.occupantDetail.contactDetail).length
Oxi
  • 2,918
  • 17
  • 28
  • Can you tell me what is `a` ? Or can you show how to do that by using same object which I have used ? – Sampath Jun 28 '16 at 11:04
  • its the object you want check for, in your case `vm.occupantDetail.contactDetail` – Oxi Jun 28 '16 at 11:05
0

It appears from your code that your vm.occupantDetail.contactDetail object is simply an empty object, and the __proto__ property that you are seeing is the protoype property of the Object. If you want to check if an object is null, the following conditional will do the job.

if (obj == null) { ... }

However, it appears that you want to check if an object is empty, which is different. If you want to check if a specified object has no assigned properties, try the following function.

function isEmpty(map) {
   for(var key in map) {
      if (map.hasOwnProperty(key)) {
         return false;
      }
   }
   return true;
}
Patrick Bell
  • 769
  • 3
  • 15
0

Check the length of your object also the length of your keys.

        if (Object.keys(vm.occupantDetail.contactDetail).length > 0)
        { 
            // vm.occupantDetail.contactDetail has values
        }
M.Tanzil
  • 1,987
  • 1
  • 12
  • 28
0

check it by jQuery.isEmptyObject()

jQuery.isEmptyObject({}); // true
jQuery.isEmptyObject({ foo: "bar" });

https://api.jquery.com/jQuery.isEmptyObject/

bhupesh
  • 104
  • 9