2015-02-11 99 views
0

鑑於我願意的話,它看起來像structuredObj遞歸構建它的對象數據的對象。我有s1到sN。我只定義了s2開始。結構使用遞歸

這是輸入:

    var data { 
         ... 

         "s2": true, 
         "s2a": true, 
         "s2b": true, 
         "s2c": true, 
         "s2d": true, 
         "s2e": true, 
         "s2f": true, 
         "s2fa": true, 
         "s2fb": false, 
         "s2g": true, 
         "s2h": true, 
         "s2ha": false, 
         "s2hb": true, 
         "s2i": true, 
         "s2ia": false, 
         "s2j": true, 
         "s2k": true, 
         "s2l": true, 
         "s2m": true, 
         "s2n": true, 
         "s2o": true 

         ... 
        } 

這是輸出:

var structuredObj = { 
      "s2": { 
      "situation": "The client owns a property.", 
       "a": true, 
       "questions": { 
       "a": { 
        "q": "A. Does the court order permit the deputy to sell the property?", 
         "a": true 
       }, 
       "b": { 
        "q": "B. Is the client a resident in the property?", 
         "a": true 
       }, 
       "c": { 
        "q": "C. Is the spouse of the client a resident in the property?", 
         "a": true 
       }, 
       "d": { 
        "q": "D. Is a dependant of the client a resident in the property?", 
         "a": true 
       }, 
       "e": { 
        "q": "E. Is the property being maintained?", 
         "a": true 
       }, 
       "f": { 
        "q": "F. Is the property being rented out?", 
         "a": true, 
         "questions": { 
         "a": { 
          "q": "a) Is an appropriate income being received?", 
           "a": true 
         }, 
         "b": { 
          "q": "b) Is there a rental agreement in place?", 
           "a": false 
         } 
        } 
       }, 
       "g": { 
        "q": "G. Is there an appropriate insurance policy in place?", 
         "a": true 
       }, 
       "h": { 
        "q": "H. Does the client have any valuables in the property?", 
         "a": true, 
         "questions": { 
         "a": { 
          "q": "a) Are the valuables secure?", 
           "a": false 
         }, 
         "b": { 
          "q": "b) Have details of the valuable been reported to the OPG?", 
           "a": true 
         } 
        } 
       }, 
       "i": { 
        "q": "I. Does the client require care?", 
         "a": true, 
         "questions": { 
         "a": { 
          "q": "a) Are the client’s care costs being met?", 
           "a": false 
         } 
        } 
       }, 
       "j": { 
        "q": "J. Is there a charge against the property?", 
         "a": true 
       }, 
       "k": { 
        "q": "K. Is the deputy considering selling the property?", 
         "a": true 
       }, 
       "l": { 
        "q": "L. Is the property jointly owned?", 
         "a": true 
       }, 
       "m": { 
        "q": "M. Has the Deputy been asked to take action previously?", 
         "a": true 
       }, 
       "n": { 
        "q": "N. Is there a history of risk on the case?", 
         "a": true 
       }, 
       "o": { 
        "q": "O. Have any other risks to the client been identified?", 
         "a": true 
       } 
      } 
     } 



        // Something like this: 
        structuredObj = structureMyFlatObject(data); 

這是接受輸入和結構它的功能。

  structureData: function (data) { 

       var i=0, 
        situationNum, 
        previousSituationLetter, 
        situationLetter, 
        situationalChildLetter, 
        label = 'LABEL.NAME', 
        structuredObj = {}, 
        keys = Object.keys(data); 

       for(; i<keys.length; i++) { 

        situationNum = keys[i].substring(0,2); // s1 - s28 
        situationLetter = keys[i].charAt(2); // a - z 

        if(keys[i].length === 4) { 

         situationalChildLetter = keys[i].charAt(3); // a - z 

         if(!structuredObj[situationNum].questions[previousSituationLetter].questions) { 
          structuredObj[situationNum].questions[previousSituationLetter].questions = {}; 
         } 

         structuredObj[situationNum].questions[previousSituationLetter].questions[situationalChildLetter] = { 
          "q": $translate.instant(label + keys[i].toUpperCase()), 
          "a": data[keys[i]] 
         }; 

         continue; 
        } 

        //Does the property start with s1, s2, s3, .., .., s28 
        if(keys[i].indexOf(situationNum) === 0) { 

         if(structuredObj[situationNum]) { 
          structuredObj[situationNum].questions[situationLetter] = { 
           "q": $translate.instant(label + keys[i].toUpperCase()), 
           "a": data[keys[i]] 
          }; 
         } else { 
          structuredObj[situationNum] = {}; 
          structuredObj[situationNum].situation = $translate.instant(label + keys[i].toUpperCase()); 
          structuredObj[situationNum].a = data[keys[i]]; 
          structuredObj[situationNum].questions = {}; 
         } 
        } 

        previousSituationLetter = situationLetter; 
       } 

       return structuredObj; 
      }, 

這適用於這一特定使用案例,但我需要它爲所有e.g S1-S28的工作。例如,當碰到s10fa時,這個函數就會爆炸。我希望有一個遞歸解決方案,因爲它開始變得混亂。

感謝

+0

請告訴我們你設法寫什麼的隨機X-量,否則我們將無法幫助你吧。 – Bergi 2015-02-11 19:01:11

+0

按要求:-) – user3034151 2015-02-12 12:20:56

回答

0

爲什麼不出去狀結構,而是隻有兩個節點的二叉樹,具有迭代節點的X-金額,只是遍歷每個人,做財產以後,以確定它是否將是一個問題或者是一個新的自我對象。

編輯:我的意思是這樣的:

var max_depth = 4; 
 
    var current_depth = 0; 
 

 
    function n(){ 
 
     return {}; 
 
    } 
 

 
    function random_object(obj) { 
 
     if (obj == null) { 
 
      temp = {}; 
 
      var end = Math.floor((Math.random() * 10) + 1); 
 
      for (i = 0; i < end; i++) { 
 
      temp[String(i)] = null; 
 
      } 
 
      return temp; 
 

 
     } else { 
 
      for (var key in obj) { 
 
       if((Math.random() * 10) < 5){ 
 
       obj[key] = "This is element " + key + " of depth " + current_depth 
 
       }else{ 
 
       if(current_depth < max_depth){ 
 
        current_depth = current_depth + 1 
 
        obj[key] = random_object(random_object()); 
 
       }else{ 
 
        obj[key] = "This is element " + key + " of depth " +  (current_depth + 1) 
 
       } 
 
       } 
 
      } 
 
      current_depth = current_depth - 1 
 
      return obj; 
 
     } 
 
    } 
 
    console.log(random_object(random_object()));

它遞歸生成屬性的隨機的x量的目的,其中每個可以是一個字符串(問題)或另一物體屬性

+0

不,它必須用javascript編寫,轉換成json然後通過電線。 – user3034151 2015-02-11 17:32:05

+0

感謝您的回答,但是,您還沒有真正回答我的問題。輸入必須是數據(像我發佈)我無法控制這個。同樣,結構必須看起來像我發佈的。中間沒有。謝謝。 – user3034151 2015-02-11 20:31:38

+0

我不是在爲你做你的工作,我展示了一個使用遞歸函數來構建對象的例子。您需要了解如何格式化數據並遞歸構建到對象中。我會給你一個提示,你需要弄清楚只有兩種可能的反應的if語句在哪裏發生;一個要麼設置一個值,要麼導致函數遞歸地被調用,返回一個對象。然而,你的例子中的對象沒有一致性(「問題」和「f」都是嵌套對象),所以我實際上不能判斷什麼會放置一個值,什麼稱爲函數。 – Thermatix 2015-02-11 22:07:36