-1

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?

Don Jose
  • 1,208
  • 1
  • 12
  • 26
  • 9
    You can't, but you can do `tree.hasOwnProperty("leaves") && tree.leaves.hasOwnProperty("leaf")` – Niet the Dark Absol Oct 14 '16 at 11:03
  • 1
    Alternatively, 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 Answers2

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
  • 64,240
  • 8
  • 67
  • 88
1

You have to use something like below

    var tree = {}
    tree["leaves"]={}
    tree["leaves"]['leaf'] = {}
    tree.leaves.hasOwnProperty("leaf")
mymotherland
  • 7,740
  • 13
  • 64
  • 120