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

3

4 Answers 4

3

You can find the it using

Object.keys(vm.occupantDetail.contactDetail).length
Sign up to request clarification or add additional context in comments.

2 Comments

Can you tell me what is a ? Or can you show how to do that by using same object which I have used ?
its the object you want check for, in your case vm.occupantDetail.contactDetail
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;
}

2 Comments

Wouldn't it be easier to do Object.getOwnPropertyNames(obj).length === 0?
@domyos I think that may run into issues if you extend the Object via prototype, I'm not 100% sure though.
0

check it by jQuery.isEmptyObject()

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

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

Comments

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
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.