如果我有一个对象的引用:
var test = {};
that will potentially (but not immediately) have nested objects, something like:
{level1: {level2: {level3: "level3"}}};
What is the best way to check for the existence of property in deeply nested objects?
alert(test.level1);
yields undefined
, but alert(test.level1.level2.level3);
fails.
I’m currently doing something like this:
if(test.level1 && test.level1.level2 && test.level1.level2.level3) {
alert(test.level1.level2.level3);
}
but I was wondering if there’s a better way.
我编写了自己的函数,该函数采用了所需的路径,并且具有好坏的回调函数。
用法: