2017-05-18 11 views
1

我有一個多級對象,其中定義了不同的對象。其中一些屬性的「葉子」是默認值,並且包含一個默認值。探索並獲取多級對象的屬性

我需要在javascript中構造另一個對象,具有與葉子相同的「路徑」,但默認值爲屬性的值。

對於同一個「樓層」上的所有對象都可以,對象上有簡單對象和hasOwnProperty。但在這裏,在我看來,我需要探索對象的時間更長,而且我不知道如何解決對象,因爲我不能使用像object [iter1.iter2.iter3 ...] 。 我需要一提的是我工作的一個服務現在服務器在尤里卡版本,所以它僅支持ECMAScript的3

有一個例子:

  • OBJET我需要處理( JSON編碼):

    "schema": {"properties": { 
        "videoCapability": { 
         "default": "1", 
         "type": [ 
          "string" 
         ], 
         "choices": [ 
          { 
           "value": "0", 
           "title": "Disabled" 
          }, 
          { 
           "value": "1", 
           "title": "Enabled" 
          } 
         ],   }, 
        "ice": { 
         "properties": { 
          "iceCapability": { 
           "default": "1", 
           "type": [ 
            "string" 
           ], 
           "choices": [ 
            { 
             "value": "0", 
             "title": "Disabled" 
            }, 
            { 
             "value": "1", 
             "title": "Enabled" 
            } 
           ], 
           "name": "iceCapability"," 
          }, 
          "defaultcandidatetype": { 
           "default": "0", 
           "type": [ 
            "string" 
           ], 
           "choices": [ 
            { 
             "value": "0", 
             "title": "Host" 
            }, 
            { 
             "value": "1", 
             "title": "Server Reflexive" 
            }, 
            { 
             "value": "2", 
             "title": "Relayed" 
            } 
           ], 
          }, 
        etc. 
    
  • 對象我需要建立:

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

如果你想知道爲什麼我要做到這一點,它是第一個對象是JSON從遠程服務器retreived。在將這個json編碼到一個對象後,我需要修改一些屬性,但不是全部。我需要將對象(已解碼)發送回遠端服務器。

+0

您的「我需要構建的對象」是無效的語法。你的意思是'{「config.properties.videoCapability」:「1」}'? –

回答

0

lodash.get可能是有用的位置:

鑑於這種對象: { a: { b: { c: { d: { e: { f: { g: {h : 1}}}}}}}}

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

如果沒有定義這些對象之一,該操作將返回undefined而不是拋出。

0

我終於找到了一個通過推廣一個屬性解決方案而不添加外部庫的解決方案。它給出了一個遞歸函數,返回一個新的對象:

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; 
    } 
},