如何在javascript中检查object.subobject.subsubobject是否存在?

时间:2015-07-13 04:17:01

标签: javascript serialization javascript-objects

层次结构可能非常深,那么如何检查属性是否存在于根对象中的某个位置?如果没有,那就创建它。

我有以下html

<div id="container">
   <div id="level-1">
       <input id="value-1" group="a.b" name="c" value="some-value-1">
   </div>
</div>

现在我想根据父属性将此输入的值放入javascript对象中。

所需的输出

{
     "a" : {
       "b" : {
         "c" : "some-value-1"
       } 
    }

}

我的努力:

function traverse(id, obj) {
    var methodName = "traverse";
    var elementToTraverse = document.getElementById(id);
    var currentElementLength = elementToTraverse.childNodes.length;

    if (currentElementLength > 0) {

        var children = getChildNodes(elementToTraverse);
        for (var ch in children) {
            var currentChild = children[ch];
            //ignore the text nodes
            if (currentChild.nodeType == 3) {
                continue;
            }

            if (currentChild.nodeType == 1 && currentChild.childNodes.length > 0 && currentChild.id != "") {
                //call without the object argument as it has already been constructed.
                traverse(currentChild.id, obj);
            }
            else if (currentChild.nodeType == 1 && currentChild.id != "" && currentChild.getAttribute('name') != null) {
                if (DEBUG) {
                    logOnConsole(methodName, currentChild.getAttribute('name') + "--" + currentChild.id, logLevel.INFO);
                }
                var group = currentChild.getAttribute('group') || null;
                var name = currentChild.getAttribute('name');
                var value = getValue(currentChild);

                if (value == "" || value == undefined) {
                    if(group){
                        if(isNull(obj[group])){
                            obj[group] = new Object();
                        }
                        obj[group][name] = "";
                    }
                    else{
                        obj[name] = "";
                    }
                }
                else if(group){
                    if(isNull(obj[group])){
                        obj[group] = new Object();
                    }
                    obj[group][name] = value;
                }
                else {
                    obj[name] = value;
                }

            }
            else {

                if (DEBUG) {
                    logOnConsole(methodName, "Element not useful. \n" + currentChild.nodeName, logLevel.INFO);
                }
            }
        }
    }
    return obj;
}

我通过遍历(&#39; container-id&#39;,new Object())调用它,但这将适用于组中的单个值而不是嵌套结构。< / p>

1 个答案:

答案 0 :(得分:0)

试试这个

&#13;
&#13;
function isExist(obj, path) {
   patharray = path.split(".");
   for(var i=0;i < patharray.length; i++) {
       obj = obj[patharray[i]];
       if(obj === undefined) return false
   }
   return true;
}

document.body.innerHTML = isExist({subobject:{subsubobject:{test: 34}}}, 'subobject.subsubobject');
&#13;
&#13;
&#13;