Tuesday, 15 February 2011

javascript - How to check if a property contains a property -


I should know if property is property or not, I just like the examples, if I < P> is an example object

  var foo = {"bar": {"foobar": {"barfoo": 1}}}   

and I have to check whether barfoo is present, but first I have to check that there are all other properties, because it can happen or not if any other function is running. I can not really reduce the level of this structure, so there is no alternative.

Currently I am doing something like this:

  var d = Foo; If (D) {d = d.bar; If (d) {d = d. Fobar; If (D) {d = d.barfoo; If (d) {console.log (d); More Strong Solution: Check  any type of object    

  var Foo = {"Bar": {"foobar": {"barfoo": {"ddd": 1}}}}; Function Checkpops (OBJ) {var ref = obj, arg, i; (I = 1; i & lt; arguments.length; i + = 1) {arg = argument for [i]; If (ref.hasOwnProperty (arg)) {ref = ref [arg]} and {return false; True} true; } // function takes the object to search the first parameter and any other properties [, prop1, prop2, ...] as console.log (checkProps (foo, 'bar', 'foobar', 'barfoo' , 'Ddd'));    

No comments:

Post a Comment