Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 探索和拾取多级对象的属性_Javascript_Json_Object - Fatal编程技术网

Javascript 探索和拾取多级对象的属性

Javascript 探索和拾取多级对象的属性,javascript,json,object,Javascript,Json,Object,我有一个多级对象,其中包含不同对象的定义。其中一些属性“leafs”是默认值,并包含默认值 我需要用javascript构建另一个对象,该对象与叶的“路径”相同,但默认值作为属性值 同一“楼层”上的所有对象都可以,对象上有一个简单的for和hasOwnProperty。但在这里,在我看来,我需要花更多的时间来探索这个对象,我不知道如何处理这个对象,因为我不能使用类似对象的东西[iter1.iter2.iter3…]。 我需要提到的是,我正在使用Eureka版本的Service Now服务器,因此

我有一个多级对象,其中包含不同对象的定义。其中一些属性“leafs”是默认值,并包含默认值

我需要用javascript构建另一个对象,该对象与叶的“路径”相同,但默认值作为属性值

同一“楼层”上的所有对象都可以,对象上有一个简单的for和hasOwnProperty。但在这里,在我看来,我需要花更多的时间来探索这个对象,我不知道如何处理这个对象,因为我不能使用类似对象的东西[iter1.iter2.iter3…]。 我需要提到的是,我正在使用Eureka版本的Service Now服务器,因此它只支持ECMAScript 3

有一个例子:

  • 我需要处理的对象(json编码):

  • 我需要构建的对象:

                "config": { config.properties.videoCapability = "1"
                    config.properties.ice.properties.iceCapability = "1"
                    config.properties.ice.properties.defaultcandidatetype = "0"
                }
    
等等


如果您想知道我为什么需要这样做,那就是第一个对象是从远程服务器以json格式检索的。将此json编码为对象后,我需要使用rigth值修改一些属性,但不是全部。我需要将对象(已解码)发送回远程服务器。

lodash.get
在这里很有用:

鉴于这一目标:
{a:{b:{c:{d:{e:{f:{g:{h:1}}}

\uj.get(obj'a.b.c.d.e.f.g.h')
返回1


如果其中一个对象未定义,则操作将返回
未定义的
,而不是抛出。

lodash.get
在这里很有用:

鉴于这一目标:
{a:{b:{c:{d:{e:{f:{g:{h:1}}}

\uj.get(obj'a.b.c.d.e.f.g.h')
返回1


如果其中一个对象未定义,则操作将返回
未定义的
,而不是抛出。

我最终找到了一个解决方案,方法是在不添加外部库的情况下泛化一个属性解决方案。它提供了一个递归函数,返回一个新对象:

exploreObject : function (source, target, value, n) {
    if(n > 10000) // just to avoid infinite loop
        return;

    for(var i in source) {
    //for every property in the source
        if(source[i].hasOwnProperty('default')){
        // if the property contains default values, set the same property in the target object to the default values 
            target[i] = source[i]['default'];
            if (i == 'videoCapability' || i == 'ciscoCamera'){
            //in my case, I had properties that needed to be changed from default to the 'value' value
                target[i] = value;
            }
        } else if (typeof source[i] == 'object' && source[i] != null ){
        //if the property doesn't contains default values and if it is an object, then we have to inspect this object
            if (i == 'properties' && !NbPair(n)) {
            //With JSON object, it can contains artefact property (with impair values of 'deepness' n), because coding to JSON can add a property 'properties' to store the properties. We don't need it, the values will be added to target object and not to target.properties
                target = this.exploreObject(source[i], target, value, n + 1);
            } else {
            //target[i] has to be an object to store the new properties
                target[i] = {};
                target[i] = this.exploreObject(source[i], target[i], value, n + 1);
            }
            if (typeof target[i] == 'object' && isEmpty(target[i]) == true){
            //After the call to the function itself, we delete the newly added object if it is empty (did not contained any default property, or the default value was empty)
                delete target[i];
            }
        }
    }

    return target; //return the object with correct values

    function isEmpty(obj) {
    //function used to check if the object is empty
        for(var key in obj) {
            if(obj.hasOwnProperty(key))
                return false;
        }
        return true;
    }
    function NbPair(Nb){
    //function used to check if the 'deepness' in the object is pair or impair
        if(Nb/2 == Math.round(Nb/2))
            return true;
        else
            return false;
    }
},

我最终通过在不添加外部库的情况下推广单属性解决方案找到了一个解决方案。它提供了一个递归函数,返回一个新对象:

exploreObject : function (source, target, value, n) {
    if(n > 10000) // just to avoid infinite loop
        return;

    for(var i in source) {
    //for every property in the source
        if(source[i].hasOwnProperty('default')){
        // if the property contains default values, set the same property in the target object to the default values 
            target[i] = source[i]['default'];
            if (i == 'videoCapability' || i == 'ciscoCamera'){
            //in my case, I had properties that needed to be changed from default to the 'value' value
                target[i] = value;
            }
        } else if (typeof source[i] == 'object' && source[i] != null ){
        //if the property doesn't contains default values and if it is an object, then we have to inspect this object
            if (i == 'properties' && !NbPair(n)) {
            //With JSON object, it can contains artefact property (with impair values of 'deepness' n), because coding to JSON can add a property 'properties' to store the properties. We don't need it, the values will be added to target object and not to target.properties
                target = this.exploreObject(source[i], target, value, n + 1);
            } else {
            //target[i] has to be an object to store the new properties
                target[i] = {};
                target[i] = this.exploreObject(source[i], target[i], value, n + 1);
            }
            if (typeof target[i] == 'object' && isEmpty(target[i]) == true){
            //After the call to the function itself, we delete the newly added object if it is empty (did not contained any default property, or the default value was empty)
                delete target[i];
            }
        }
    }

    return target; //return the object with correct values

    function isEmpty(obj) {
    //function used to check if the object is empty
        for(var key in obj) {
            if(obj.hasOwnProperty(key))
                return false;
        }
        return true;
    }
    function NbPair(Nb){
    //function used to check if the 'deepness' in the object is pair or impair
        if(Nb/2 == Math.round(Nb/2))
            return true;
        else
            return false;
    }
},
您的“需要生成的对象”语法无效。您的意思是
{“config.properties.videoCapability”:“1”}
?您的“我需要构建的对象”语法无效。你是说
{“config.properties.videoCapability”:“1”}