フラットなJSONをツリー構造に変換しようとしていますが、これが可能かどうか不思議です。私の問題を解決する助けがあれば幸いです。IDなしの階層/ツリーへのJSONフラット
以下は、私が現在試しているもので、作業中です。これは最初の子どもの集合を正常に取得するだけで、子どもです。私はそれをこの点に合格させることができませんでした。私はこれが可能でないかもしれないことを恐れる。
私が始めた前に私は根を知っています。私はそれが可能でなければならないと思います。このケースでは、ルートは「自動軍用ヘルスシステム機能を提供する」である。
JSONは、のように見える終わるでしょうツリー:ここ
{ "name": "Provide Automated Military Health Systems Functions", "children": [ { "name": "Provide Infrastructure Support Functions", "children": [ "name": "Provide Data Management Functions" "children": [etc, etc] ] }, { "name": "Provide Clinical Support Functions", "children": [etc etc] }, { "name": "Provide Non-Clinical Support Functions", "children": [etc etc] } ] }
は私ものオフに取り組んできたバイオリンです:http://jsfiddle.net/ydgbkv39/
フィドルは、最初の2のコンソール印刷を行いますレベルを超えているようには思えません。
誰かが私を助けてくれることを願っています。
var data = [
{
"Upstream": "Provide Automated Military Health Systems Functions",
"Downstream": "Provide Infrastructure Support Functions"
},
{
"Upstream": "Provide Automated Military Health Systems Functions",
"Downstream": "Provide Clinical Support Functions"
},
{
"Upstream": "Provide Automated Military Health Systems Functions",
"Downstream": "Provide Non-Clinical Support Functions"
},
{
"Upstream": "Provide Infrastructure Support Functions",
"Downstream": "Provide Data Management Functions"
},
{
"Upstream": "Provide Infrastructure Support Functions",
"Downstream": "Provide Maintenance Utilities Functions"
},
{
"Upstream": "Provide Infrastructure Support Functions",
"Downstream": "Provide Business Rules Execution Functions"
},
{
"Upstream": "Provide Infrastructure Support Functions",
"Downstream": "Provide MHS Health Portal Functions"
},
{
"Upstream": "Provide Infrastructure Support Functions",
"Downstream": "Provide Security Functions"
},
{
"Upstream": "Provide Clinical Support Functions",
"Downstream": "Provide Care Management Functions"
},
{
"Upstream": "Provide Clinical Support Functions",
"Downstream": "Provide Nutrition Information Management Functions"
},
{
"Upstream": "Provide Clinical Support Functions",
"Downstream": "Provide Healthcare Specialty Services Functions"
},
{
"Upstream": "Provide Clinical Support Functions",
"Downstream": "Provide Lab Support Functions"
},
{
"Upstream": "Provide Clinical Support Functions",
"Downstream": "Provide Pharmacy Support Functions"
},
{
"Upstream": "Provide Clinical Support Functions",
"Downstream": "Provide Blood Management Functions"
},
{
"Upstream": "Provide Clinical Support Functions",
"Downstream": "Provide Medical Imagery Support Functions"
},
{
"Upstream": "Provide Clinical Support Functions",
"Downstream": "Provide Operations Support Functions"
},
{
"Upstream": "Provide Clinical Support Functions",
"Downstream": "Provide Order Results Care Management Functions"
},
{
"Upstream": "Provide Clinical Support Functions",
"Downstream": "Provide Medical Orders Maintenance Functions"
},
{
"Upstream": "Provide Clinical Support Functions",
"Downstream": "Provide Episodes of Care Functions"
},
{
"Upstream": "Provide Non-Clinical Support Functions",
"Downstream": "Provide Executive Decision Support Management Functions"
},
{
"Upstream": "Provide Non-Clinical Support Functions",
"Downstream": "Provide Manage Family Support Process Workflow (BEA)"
},
{
"Upstream": "Provide Non-Clinical Support Functions",
"Downstream": "Provide Health Records Support Functions"
},
{
"Upstream": "Provide Non-Clinical Support Functions",
"Downstream": "Provide Resource Management Functions"
},
{
"Upstream": "Provide Non-Clinical Support Functions",
"Downstream": "Provide Medical Readiness Management Functions"
},
{
"Upstream": "Provide Non-Clinical Support Functions",
"Downstream": "Provide Population Health Management Functions"
},
{
"Upstream": "Provide Non-Clinical Support Functions",
"Downstream": "Provide Medical Logistics Management Functions"
},
{
"Upstream": "Provide Non-Clinical Support Functions",
"Downstream": "Provide Patient Directory Functions"
},
{
"Upstream": "Provide Non-Clinical Support Functions",
"Downstream": "Provide Provider Information Functions"
},
{
"Upstream": "Provide Non-Clinical Support Functions",
"Downstream": "Provide Patient Administration Functions"
}
];
Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};
var upstreamArr = [];
var downstreamArr = [];
data.forEach(function (a) {
\t upstreamArr.push(a.Upstream);
downstreamArr.push(a.Downstream);
}, {});
var root = upstreamArr.diff(downstreamArr);
root = root[0];
var tree = {};
tree.name = root;
tree.children = [];
data.forEach(function (a) {
\t if(a.Upstream === root) {
\t if(tree.children.indexOf(a.Downstream) === -1) {
\t tree.children.push(a.Downstream);
}
}
}, {});
function buildTree(d) {
\t if(d.children.length > 0) {
\t for(var i = 0; i < d.children.length; i++) {
\t findKids(d, d.children[i]);
}
}
return d;
}
function findKids(d, child) {
let obj = {};
obj.children = [];
\t data.forEach(function (a) {
if(a.Upstream === child) {
\t obj.name = child;
if(obj.children.indexOf(a.Downstream) === -1) {
obj.children.push(a.Downstream);
}
}
}, {});
\t var ind = d.children.indexOf(child);
\t return d.children[ind] = obj;
}
/*function eachRecursive(obj) {
for (var k in obj) {
if (typeof obj[k] == "object" && obj[k] !== null) {
\t eachRecursive(obj[k]);
} else {
}
}
}*/
console.log(buildTree(tree));
ビルトインされているため、タイプするだけのコードの評論家として、あなたは 'Array.prototype.diff'を変更することは避けてください。詳細については、[この回答を見る](https://stackoverflow.com/a/948379/266535)を参照してください。 – styfle