JSON Объект манипуляции в формате, подходящем для визуализации? - PullRequest
1 голос
/ 31 октября 2019

Я манипулирую JSON, чтобы сделать его пригодным для визуализации. Я хочу, чтобы он был в формате родительско-дочерних отношений.

Я написал фрагмент кода, который работает, но он не включает в себя супер-родитель (тот, в котором ключ parent_id имеет значение null).

var flare = createJSON(this.dataset);

function createJSON(original) {
  function getChildren(pid) {
     return original.filter(function(d) {
         return d.parent_id === pid;
     })
     .map(function(d) {
       return {
         name: d.name,
         id: d.id,
         parent_id: d.parent_id,
         value: d.value,
         logValue: d.logValue,
         children: getChildren(d.id)
       };
     });
  }

  return original.map(d=> getChildren(d.id))
}
console.log(flare)

Это моя скрипка https://jsfiddle.net/x0gs1nvz/ Он создает JSON с родительскими отношениями между детьми, но не включает в себя суперпользователей, потому что у супер-родителей parent_id есть null. Любая помощь, что я пропускаю?

Ответы [ 3 ]

1 голос
/ 31 октября 2019

Почему бы просто не начать сначала с родителя?

  return original
    .filter(d => d.parent_id === null)
    .map(d => {
      return {
        name: d.name,
        id: d.id,
        parent_id: d.parent_id,
        value: d.value,
        logValue: d.logValue,
        children: getChildren(d.id)
      };
    });

var dataset = [
  {
    id: 1,
    name: "JOURNAL",
    tree_level: 1,
    display_order: null,
    parent_or_leaf_or_both: "Parent",
    parent_id: null,
    value: 4346
  },
  {
    id: 2,
    name: "OTHERS",
    tree_level: 1,
    display_order: null,
    parent_or_leaf_or_both: "Parent",
    parent_id: null,
    value: 28
  },
  {
    id: 3,
    name: "PATENT",
    tree_level: 1,
    display_order: null,
    parent_or_leaf_or_both: "Parent",
    parent_id: null,
    value: 18526
  },
  {
    id: 4,
    name: "A",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Both",
    parent_id: 1,
    value: 144
  },
  {
    id: 5,
    name: "B",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Both",
    parent_id: 1,
    value: 1966
  },
  {
    id: 6,
    name: "C",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Both",
    parent_id: 1,
    value: 285
  },
  {
    id: 7,
    name: "D",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Both",
    parent_id: 1,
    value: 23
  },
  {
    id: 8,
    name: "E",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Both",
    parent_id: 1,
    value: 66
  },
  {
    id: 9,
    name: "F",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Both",
    parent_id: 1,
    value: 1
  },
  {
    id: 13,
    name: "I",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Both",
    parent_id: 1,
    value: 1
  },
  {
    id: 14,
    name: "J",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Both",
    parent_id: 1,
    value: 1528
  },
  {
    id: 16,
    name: "L",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Parent",
    parent_id: 1,
    value: 61
  },
  {
    id: 17,
    name: "M",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Both",
    parent_id: 1,
    value: 153
  },
  {
    id: 18,
    name: "N",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Both",
    parent_id: 1,
    value: 25
  },
  {
    id: 20,
    name: "P",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Both",
    parent_id: 1,
    value: 139
  },
  {
    id: 33,
    name: "Company",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 2,
    value: 1
  },
  {
    id: 40,
    name: "Meetings",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 2,
    value: 19
  },
  {
    id: 42,
    name: "OTHERS",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 2,
    value: 5
  },
  {
    id: 45,
    name: "Product Leaflet",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 2,
    value: 1
  },
  {
    id: 48,
    name: "Society",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 2,
    value: 1
  },
  {
    id: 52,
    name: "Websites",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 2,
    value: 3
  },
  {
    id: 53,
    name: "EP",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 3,
    value: 2004
  },
  {
    id: 55,
    name: "US",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 3,
    value: 11355
  },
  {
    id: 57,
    name: "WO",
    tree_level: 2,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 3,
    value: 5172
  },
  {
    id: 74,
    name: "ACS Med Chem Lett",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 4,
    value: 66
  },
  {
    id: 684,
    name: "Annu. Rep. Med. Chem.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 4,
    value: 19
  },
  {
    id: 863,
    name: "Arch. Pharm. (Weinheim)",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 4,
    value: 59
  },
  {
    id: 1029,
    name: "Biochem. Pharmacol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 5,
    value: 2
  },
  {
    id: 1079,
    name: "Biol. Psychiatry.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 5,
    value: 2
  },
  {
    id: 1114,
    name: "Bioorg. Med. Chem.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 5,
    value: 644
  },
  {
    id: 1115,
    name: "Bioorg. Med. Chem. Lett.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 5,
    value: 1169
  },
  {
    id: 1206,
    name: "BMC Neurosci.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 5,
    value: 1
  },
  {
    id: 1251,
    name: "Brain Res.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 5,
    value: 2
  },
  {
    id: 1309,
    name: "Br. J. Pharmacol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 5,
    value: 8
  },
  {
    id: 1361,
    name: "Bull. Korean Chem. Soc.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 5,
    value: 138
  },
  {
    id: 1573,
    name: "Cent. Nerv. Syst. Agents Med. Chem.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 6,
    value: 1
  },
  {
    id: 1599,
    name: "Chembiochem",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 6,
    value: 2
  },
  {
    id: 1605,
    name: "Chem. Biol. Drug Des.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 6,
    value: 6
  },
  {
    id: 1649,
    name: "ChemMedChem",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 6,
    value: 12
  },
  {
    id: 1785,
    name: "Clin. Exp. Gastroenterol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 6,
    value: 1
  },
  {
    id: 2072,
    name: "Curr. Drug Targets CNS Neurol. Disord.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 6,
    value: 110
  },
  {
    id: 2101,
    name: "Curr. Med. Chem.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 6,
    value: 116
  },
  {
    id: 2106,
    name: "Curr. Med. Chem. CNS. Agents.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 6,
    value: 4
  },
  {
    id: 2119,
    name: "Curr. Opin. Chem. Biol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 6,
    value: 13
  },
  {
    id: 2140,
    name: "Curr. Pharm. Des.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 6,
    value: 4
  },
  {
    id: 2152,
    name: "Curr. Top. Med. Chem.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 6,
    value: 16
  },
  {
    id: 2327,
    name: "Drug Data Rep.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 7,
    value: 19
  },
  {
    id: 2381,
    name: "Drugs Future",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 7,
    value: 5
  },
  {
    id: 2634,
    name: "Eur. J. Clin. Invest.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 8,
    value: 1
  },
  {
    id: 2658,
    name: "Eur. J. Med. Chem.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 8,
    value: 57
  },
  {
    id: 2674,
    name: "Eur. J. Pharmacol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 8,
    value: 5
  },
  {
    id: 2691,
    name: "Eur. Neuropsychopharmacol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 8,
    value: 3
  },
  {
    id: 2950,
    name: "Fundam. Clin. Pharmacol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 9,
    value: 1
  },
  {
    id: 3460,
    name: "Int. J. Neuropsychopharmacol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 13,
    value: 1
  },
  {
    id: 3770,
    name: "J. Biol. Chem.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 14,
    value: 2
  },
  {
    id: 4012,
    name: "J. Enzyme Inhib. Med. Chem.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 14,
    value: 1
  },
  {
    id: 4183,
    name: "J. Life Sci.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 14,
    value: 1
  },
  {
    id: 4206,
    name: "J. Med. Chem.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 14,
    value: 1356
  },
  {
    id: 4251,
    name: "J. Nat. Prod.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 14,
    value: 2
  },
  {
    id: 4483,
    name: "J. Pharmacol. Exp. Ther.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 14,
    value: 165
  },
  {
    id: 4552,
    name: "Jpn. J. Pharmacol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 14,
    value: 2
  },
  {
    id: 4594,
    name: "J. Psychopharmacol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 14,
    value: 3
  },
  {
    id: 4603,
    name: "J. Receptor Ligand Channel Res.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 14,
    value: 1
  },
  {
    id: 4758,
    name: "The Journal of pharmacology and experimental therapeutics",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 14,
    value: 1
  },
  {
    id: 4891,
    name: "Lett. Drug Des. Discov.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 16,
    value: 61
  },
  {
    id: 4986,
    name: "Medchemcomm.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 17,
    value: 8
  },
  {
    id: 5049,
    name: "Med. Res. Rev.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 17,
    value: 1
  },
  {
    id: 5147,
    name: "Mini Rev. Med. Chem.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 17,
    value: 64
  },
  {
    id: 5197,
    name: "Molecules",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 17,
    value: 4
  },
  {
    id: 5221,
    name: "Mol. Pharmacol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 17,
    value: 76
  },
  {
    id: 5283,
    name: "Nat. Cell Biol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 18,
    value: 2
  },
  {
    id: 5285,
    name: "Nat. Chem. Biol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 18,
    value: 4
  },
  {
    id: 5346,
    name: "Naunyn Schmiedebergs Arch. Pharmacol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 18,
    value: 2
  },
  {
    id: 5413,
    name: "Neuropharmacology",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 18,
    value: 7
  },
  {
    id: 5422,
    name: "Neuropsychopharmacology",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 18,
    value: 8
  },
  {
    id: 5426,
    name: "Neuroreport",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 18,
    value: 1
  },
  {
    id: 5537,
    name: "Nuclear medicine and biology",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 18,
    value: 1
  },
  {
    id: 5549,
    name: "Nucl. Med. Biol.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 18,
    value: 1
  },
  {
    id: 5844,
    name: "Pharmacol. Biochem. Behav.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 20,
    value: 1
  },
  {
    id: 5846,
    name: "Pharmacological reports : PR",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 20,
    value: 1
  },
  {
    id: 5862,
    name: "Pharmacol. Rev.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 20,
    value: 20
  },
  {
    id: 6136,
    name: "Proc. Natl. Acad. Sci. U.S.A.",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 20,
    value: 116
  },
  {
    id: 6247,
    name: "Psychopharmacology (Berl.)",
    tree_level: 3,
    display_order: null,
    parent_or_leaf_or_both: "Leaf",
    parent_id: 20,
    value: 1
  }
];

var flare = createJSON(dataset);

function createJSON(original) {
  function getChildren(pid) {
    return original
      .filter(function(d) {
        return d.parent_id === pid;
      })
      .map(function(d) {
        return {
          name: d.name,
          id: d.id,
          parent_id: d.parent_id,
          value: d.value,
          logValue: d.logValue,
          children: getChildren(d.id)
        };
      });
  }

  return original
    .filter(d => d.parent_id === null)
    .map(d => {
      return {
        name: d.name,
        id: d.id,
        parent_id: d.parent_id,
        value: d.value,
        logValue: d.logValue,
        children: getChildren(d.id)
      };
    });
}


console.log(flare);
1 голос
/ 31 октября 2019

var dataset = [{"id":1,"name":"JOURNAL","tree_level":1,"display_order":null,"parent_or_leaf_or_both":"Parent","parent_id":null,"value":4346},{"id":2,"name":"OTHERS","tree_level":1,"display_order":null,"parent_or_leaf_or_both":"Parent","parent_id":null,"value":28},{"id":3,"name":"PATENT","tree_level":1,"display_order":null,"parent_or_leaf_or_both":"Parent","parent_id":null,"value":18526},{"id":4,"name":"A","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Both","parent_id":1,"value":144},{"id":5,"name":"B","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Both","parent_id":1,"value":1966},{"id":6,"name":"C","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Both","parent_id":1,"value":285},{"id":7,"name":"D","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Both","parent_id":1,"value":23},{"id":8,"name":"E","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Both","parent_id":1,"value":66},{"id":9,"name":"F","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Both","parent_id":1,"value":1},{"id":13,"name":"I","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Both","parent_id":1,"value":1},{"id":14,"name":"J","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Both","parent_id":1,"value":1528},{"id":16,"name":"L","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Parent","parent_id":1,"value":61},{"id":17,"name":"M","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Both","parent_id":1,"value":153},{"id":18,"name":"N","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Both","parent_id":1,"value":25},{"id":20,"name":"P","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Both","parent_id":1,"value":139},{"id":33,"name":"Company","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":2,"value":1},{"id":40,"name":"Meetings","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":2,"value":19},{"id":42,"name":"OTHERS","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":2,"value":5},{"id":45,"name":"Product Leaflet","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":2,"value":1},{"id":48,"name":"Society","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":2,"value":1},{"id":52,"name":"Websites","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":2,"value":3},{"id":53,"name":"EP","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":3,"value":2004},{"id":55,"name":"US","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":3,"value":11355},{"id":57,"name":"WO","tree_level":2,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":3,"value":5172},{"id":74,"name":"ACS Med Chem Lett","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":4,"value":66},{"id":684,"name":"Annu. Rep. Med. Chem.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":4,"value":19},{"id":863,"name":"Arch. Pharm. (Weinheim)","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":4,"value":59},{"id":1029,"name":"Biochem. Pharmacol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":5,"value":2},{"id":1079,"name":"Biol. Psychiatry.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":5,"value":2},{"id":1114,"name":"Bioorg. Med. Chem.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":5,"value":644},{"id":1115,"name":"Bioorg. Med. Chem. Lett.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":5,"value":1169},{"id":1206,"name":"BMC Neurosci.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":5,"value":1},{"id":1251,"name":"Brain Res.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":5,"value":2},{"id":1309,"name":"Br. J. Pharmacol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":5,"value":8},{"id":1361,"name":"Bull. Korean Chem. Soc.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":5,"value":138},{"id":1573,"name":"Cent. Nerv. Syst. Agents Med. Chem.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":6,"value":1},{"id":1599,"name":"Chembiochem","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":6,"value":2},{"id":1605,"name":"Chem. Biol. Drug Des.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":6,"value":6},{"id":1649,"name":"ChemMedChem","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":6,"value":12},{"id":1785,"name":"Clin. Exp. Gastroenterol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":6,"value":1},{"id":2072,"name":"Curr. Drug Targets CNS Neurol. Disord.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":6,"value":110},{"id":2101,"name":"Curr. Med. Chem.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":6,"value":116},{"id":2106,"name":"Curr. Med. Chem. CNS. Agents.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":6,"value":4},{"id":2119,"name":"Curr. Opin. Chem. Biol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":6,"value":13},{"id":2140,"name":"Curr. Pharm. Des.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":6,"value":4},{"id":2152,"name":"Curr. Top. Med. Chem.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":6,"value":16},{"id":2327,"name":"Drug Data Rep.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":7,"value":19},{"id":2381,"name":"Drugs Future","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":7,"value":5},{"id":2634,"name":"Eur. J. Clin. Invest.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":8,"value":1},{"id":2658,"name":"Eur. J. Med. Chem.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":8,"value":57},{"id":2674,"name":"Eur. J. Pharmacol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":8,"value":5},{"id":2691,"name":"Eur. Neuropsychopharmacol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":8,"value":3},{"id":2950,"name":"Fundam. Clin. Pharmacol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":9,"value":1},{"id":3460,"name":"Int. J. Neuropsychopharmacol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":13,"value":1},{"id":3770,"name":"J. Biol. Chem.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":14,"value":2},{"id":4012,"name":"J. Enzyme Inhib. Med. Chem.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":14,"value":1},{"id":4183,"name":"J. Life Sci.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":14,"value":1},{"id":4206,"name":"J. Med. Chem.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":14,"value":1356},{"id":4251,"name":"J. Nat. Prod.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":14,"value":2},{"id":4483,"name":"J. Pharmacol. Exp. Ther.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":14,"value":165},{"id":4552,"name":"Jpn. J. Pharmacol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":14,"value":2},{"id":4594,"name":"J. Psychopharmacol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":14,"value":3},{"id":4603,"name":"J. Receptor Ligand Channel Res.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":14,"value":1},{"id":4758,"name":"The Journal of pharmacology and experimental therapeutics","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":14,"value":1},{"id":4891,"name":"Lett. Drug Des. Discov.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":16,"value":61},{"id":4986,"name":"Medchemcomm.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":17,"value":8},{"id":5049,"name":"Med. Res. Rev.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":17,"value":1},{"id":5147,"name":"Mini Rev. Med. Chem.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":17,"value":64},{"id":5197,"name":"Molecules","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":17,"value":4},{"id":5221,"name":"Mol. Pharmacol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":17,"value":76},{"id":5283,"name":"Nat. Cell Biol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":18,"value":2},{"id":5285,"name":"Nat. Chem. Biol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":18,"value":4},{"id":5346,"name":"Naunyn Schmiedebergs Arch. Pharmacol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":18,"value":2},{"id":5413,"name":"Neuropharmacology","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":18,"value":7},{"id":5422,"name":"Neuropsychopharmacology","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":18,"value":8},{"id":5426,"name":"Neuroreport","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":18,"value":1},{"id":5537,"name":"Nuclear medicine and biology","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":18,"value":1},{"id":5549,"name":"Nucl. Med. Biol.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":18,"value":1},{"id":5844,"name":"Pharmacol. Biochem. Behav.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":20,"value":1},{"id":5846,"name":"Pharmacological reports : PR","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":20,"value":1},{"id":5862,"name":"Pharmacol. Rev.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":20,"value":20},{"id":6136,"name":"Proc. Natl. Acad. Sci. U.S.A.","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":20,"value":116},{"id":6247,"name":"Psychopharmacology (Berl.)","tree_level":3,"display_order":null,"parent_or_leaf_or_both":"Leaf","parent_id":20,"value":1}];

 var flare = createJSON(dataset);

            function createJSON(original) {

                  function getChildren(pid) {
                      var result= original.filter(function(d) { 
                          return d.parent_id === pid;
                      })
                      .map(function(d) {
                          return {
                          name: d.name,
                          id: d.id,
                          parent_id: d.parent_id,
                          value: d.value,
                          logValue: d.logValue,
                          children: getChildren(d.id)
                          };
                      });
                      
                      if(result.length>0)
                      return result;
                      else{
                       return original.filter(function(d) { 
                          return d.id === pid;
                      })
                      }
                      
                  }

            	return original.map(d=> getChildren(d.id))
          }
        console.log(flare)
        document.getElementById("data").innerHTML = JSON.stringify(flare);
        
<div id="data">
 
</div>

Пожалуйста, отметьте это

0 голосов
/ 01 ноября 2019

Я нашел еще одно решение для того же, что и ниже, и это просто и приятно с меньшим количеством строк кода.

//create a name: node map
var dataMap = dataset.reduce(function(map, node) {
    map[node.id] = node;
    return map;
}, {});

//create the tree array
var tree = [];
dataset.forEach(function(node) {
//add to parent
var parent = dataMap[node.parent_id];
if (parent) {

   //create child array if it doesn't exist
   (parent.children || (parent.children = []))

   //add node to child array
       .push(node);
   } else {
       //parent is null or missing
       tree.push(node);
   }
});
// console.log(tree)
...