Je suis désolé si je pose une question stupide, mais c'est quelque chose qui me préoccupe et je ne suis pas encore en mesure de trouver la meilleure solution.

J'ai des données JSON qui ressemblent à ceci:

{
    "my_data": [
        {
            "name": "bugs_db",
            "type": "database",
            "children": [
                {
                    "name": "oss",
                    "type": "ui"
                },
                {
                    "name": "dashboard",
                    "type": "ui"
                },
                {
                    "name": "dev-dash",
                    "type": "ui"
                }
            ]
        },
        {
            "name": "oss",
            "type": "ui",
            "children": [
                {
                    "name": "active-directory",
                    "type": "nfs"
                },
                {
                    "name": "passive-directory",
                    "type": "FAT32"
                }
            ]
        },
        {
            "name": "jira_db",
            "type": "database",
            "children": [

            ]
        },
        {
            "name": "active_directory",
            "type": "nfs",
            "children": []
        }
    ]
}

J'essaie de traiter ces données de telle sorte que, pour une racine sélectionnée (nom), il y aura une relation hiérarchique des données. Pour exa. les données de résultat devraient ressembler à ceci (si j'ai sélectionné "bugs_db").

{
    "name": "bugs_db",
    "kind": "root",
    "children": [
        {
            "name": "oss",
            "type": "ui",
            "children": [
                {
                   "name": "active-directory",
                   "type": "nfs",
                   "children": []
                },
                {
                    "name": "passive-directory",
                    "type": "FAT32"
                }
            ]
        },
        {
            "name" : "dashboard",
            "type": "ui"
        },
        {
            "name": "dev-dash",
            "type": "ui"
        }
    ]
}

J'ai essayé d'écrire du code qui fonctionne jusqu'au 1er niveau ..

var selectedApp = "bugs_db";
var all_data = {
  name: selectedApp,
  type: "root",
  children: []
}
for(var i = 0; i < data.my_data.length; i++){
  var currentObj = data.my_data[i];
  if(currentObj.name == selectedApp && currentObj.children.length){
      for(var j = 0; j < currentObj.children.length; j++){
           let childObj = {
              name: currentObj.children[j].name,
              type: currentObj.children[j].type,
              children: []
           }
           allData.children.push(childObj);
      }
  }
}

Mais le code ci-dessus ne fait pas la hiérarchie supplémentaire. Je sais que cela peut être fait en utilisant une sorte de fonction récursive .. mais je ne sais pas comment .. c'est peut-être pourquoi tomber dans une boucle infinie.

Quelqu'un peut-il m'aider ici? Veuillez me faire savoir si vous avez besoin de plus d'informations.

3
user9397302 23 mai 2018 à 16:56

3 réponses

Meilleure réponse

Vous pouvez prendre une carte et une approche récursive en construisant de nouveaux objets.

function getTree(name) {
    var object = map.get(name);
    return object && Object.assign({}, object, { children: object.children.map(o => Object.assign({}, o, getTree(o.name))) });
}

var object = { my_data: [{ name: "bugs_db", type: "database", children: [{ name: "oss", type: "ui" }, { name: "dashboard", type: "ui" }, { name: "dev-dash", type: "ui" }] }, { name: "oss", type: "ui", children: [{ name: "active-directory", type: "nfs" }, { name: "passive-directory", type: "FAT32" }] }, { name: "jira_db", type: "database", children: [] }, { name: "active_directory", type: "nfs", children: [] }] },
    map = new Map(object.my_data.map(o => [o.name, o]));

console.log(getTree("bugs_db"));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Avec contrôle circulaire.

function getTree(name, visited = new Set) {
    var object = map.get(name);

    if (!object) {
        return object;
    }

    if (visited.has(name)) {
        return object && Object.assign({}, object, { circular: true, children: [] });
    }

    visited.add(name);
    return Object.assign({}, object, { children: object.children.map(o => Object.assign({}, o, getTree(o.name, visited))) });
}

var object = { my_data: [{ name: "bugs_db", type: "database", children: [{ name: "oss", type: "ui" }, { name: "dashboard", type: "ui" }, { name: "dev-dash", type: "ui" }, { name: "bugs_db", type: "exception" }] }, { name: "oss", type: "ui", children: [{ name: "active-directory", type: "nfs" }, { name: "passive-directory", type: "FAT32" }] }, { name: "jira_db", type: "database", children: [] }, { name: "active_directory", type: "nfs", children: [] }] },
    map = new Map(object.my_data.map(o => [o.name, o]));

console.log(getTree("bugs_db"));
.as-console-wrapper { max-height: 100% !important; top: 0; }
2
Nina Scholz 23 mai 2018 à 16:46

Tu veux quelque chose comme ça?

var data = {
    "my_data": [
        {
            "name": "bugs_db",
            "type": "database",
            "children": [
                {
                    "name": "oss",
                    "type": "ui"
                },
                {
                    "name": "dashboard",
                    "type": "ui"
                },
                {
                    "name": "dev-dash",
                    "type": "ui"
                }
            ]
        },
        {
            "name": "oss",
            "type": "ui",
            "children": [
                {
                    "name": "active-directory",
                    "type": "nfs"
                },
                {
                    "name": "passive-directory",
                    "type": "FAT32"
                }
            ]
        },
        {
            "name": "jira_db",
            "type": "database",
            "children": [

            ]
        },
        {
            "name": "active-directory",
            "type": "nfs",
            "children": [
            {
            	"name": "ubuntu",
            	"type": "os"
            }
            ]
        },
        {
            "name": "ubuntu",
            "type": "os",
            "children": []
        }
    ]
};
var selectedApp = "bugs_db";
var all_data = {
  name: selectedApp,
  type: "root",
  children: []
}
for(var i = 0; i < data.my_data.length; i++){
  var currentObj = data.my_data[i];
  if(currentObj.name == selectedApp && currentObj.children.length){
      for(var j = 0; j < currentObj.children.length; j++){
           let childObj = {
              name: currentObj.children[j].name,
              type: currentObj.children[j].type,
           }
           findChildren(childObj)
           all_data.children.push(childObj);
      }
  }
}

function findChildren(obj){
	obj.children = data.my_data.filter(o => o.name == obj.name && o.type == obj.type ).map(o => o.children)[0];
  if (obj.children)
  obj.children.forEach(child => {
  	findChildren(child);
  });
}




console.log(all_data)
0
tkdmatze 23 mai 2018 à 14:47

Tu peux faire:

const selectedApp = "bugs_db";
const data = {"my_data": [{"name": "bugs_db","type": "database","children": [{"name": "oss","type": "ui"},{"name": "dashboard","type": "ui"},{"name": "dev-dash","type": "ui"}]},{"name": "oss","type": "ui","children": [{"name": "active-directory","type": "nfs"},{"name": "passive-directory","type": "FAT32"}]},{"name": "jira_db","type": "database","children": []},{"name": "active_directory","type": "nfs","children": []}]};
const all_data = {
  name: selectedApp,
  type: "root",
  children: []
};

all_data.children = data.my_data
  .find(el => el.name == selectedApp).children
  .map(obj => {
    const found = data.my_data.find(el => el.name == obj.name);
    if (found && found.children) {
      obj.children = found.children;
    }
    return obj;
  });

console.log(all_data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
0
Yosvel Quintero Arguelles 23 mai 2018 à 14:31