I have one object var tree={} with attribute tree.leaves.leaf={}.
When I perform tree.hasOwnProperty("leaves.leaf") its giving false .
Can I use dot function inside hasOwnProperty() function? How to do it?
            Asked
            
        
        
            Active
            
        
            Viewed 732 times
        
    -1
            
            
         
    
    
        Don Jose
        
- 1,448
- 2
- 13
- 27
- 
                    9You can't, but you can do `tree.hasOwnProperty("leaves") && tree.leaves.hasOwnProperty("leaf")` – Niet the Dark Absol Oct 14 '16 at 11:03
- 
                    1Alternatively, you can use [lodash](https://lodash.com/) which has a special function for searching all nested properties: `_.get([object], '[property]');`, which in your case would look like this: `_.get(tree, 'leaf')` – rorschach Oct 14 '16 at 11:06
2 Answers
2
            
            
        If you want to create a property with key leaves.leaf, then you need to use bracket notation
tree["leaves.leaf"]={}
now tree.hasOwnProperty("leaves.leaf") will give true.
 
    
    
        gurvinder372
        
- 66,980
- 10
- 72
- 94
1
            You have to use something like below
    var tree = {}
    tree["leaves"]={}
    tree["leaves"]['leaf'] = {}
    tree.leaves.hasOwnProperty("leaf")
 
    
    
        mymotherland
        
- 7,968
- 14
- 65
- 122
- 
                    tree.leaves may be undefined for me thats why i exactly needed how i asked – Don Jose Oct 14 '16 at 11:13
- 
                    
- 
                    As per my understanding, we need to check the respective property of the object.Here leaf is property of leaves object which comes under tree object – mymotherland Oct 14 '16 at 11:33
- 
                    Please check here once http://stackoverflow.com/questions/2631001/javascript-test-for-existence-of-nested-object-key – mymotherland Oct 14 '16 at 11:37