Почему бы просто не начать сначала с родителя?
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);