使用递归构造对象

时间:2015-02-11 17:15:54

标签: javascript recursion

鉴于对象数据,我希望以递归方式构造它,使其看起来像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;
            },

这适用于这个特定的用例,但我需要它适用于所有例如s1-s28。目前,当击中s10fa时,此功能会爆炸。我希望得到一个递归的解决方案,因为它开始变得混乱。

由于

1 个答案:

答案 0 :(得分:0)

为什么不选择像结构这样的二叉树,而只选择两个节点,拥有x-amount的可迭代节点,然后迭代每一个节点,并做一些事情来确定它是一个问题还是一个问题自我的新对象。

编辑:我的意思是:

&#13;
&#13;
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()));
&#13;
&#13;
&#13;

它递归地生成一个随机x个属性的对象,每个属性可以是一个字符串(一个问题)或另一个随机x个属性的对象