鑑於我願意的話,它看起來像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時,這個函數就會爆炸。我希望有一個遞歸解決方案,因爲它開始變得混亂。
感謝
請告訴我們你設法寫什麼的隨機X-量,否則我們將無法幫助你吧。 – Bergi 2015-02-11 19:01:11
按要求:-) – user3034151 2015-02-12 12:20:56