How can I structure the final array to meet the requirements of Bootstrap Tree View for building a tree in JavaScript?

Joined
Oct 24, 2013
Messages
43
Reaction score
0
I need to generate the final array for the Bootstrap Tree View in the specified format. Currently, my code only supports nesting applications for a single level. It fails to handle multiple levels, and it also includes empty nodes unnecessarily. I want to exclude the empty nodes array when it's empty. Additionally, it's important to mention that I'm identifying applications by matching "Level One ID_1" with "Consumer ID_5".

JavaScript:
/* Bootstrap Tree View format - Sample
[
  {
    text: "Parent 1",
    nodes: [
      {
        text: "Child 1",
        nodes: [
          {
            text: "Grandchild 1"
          },
          {
            text: "Grandchild 2"
          }
        ]
      },
      {
        text: "Child 2"
      }
    ]
  },
  {
    text: "Parent 2"
  }
];
*/

//My globalApplications array in this case:
globalApplications =
[
  {
    "Con Master Contract Title_3": "aaaa",
    "Con Master Contract Type_4": "Master Licensing Agreement",
    "Pro Master Contract Title_7": "Master Contract Title - MC-024880",
    "Consumer ID_5": "10629263",
    "Level One ID_1": "10629243",
    "Pro Application Name_6": "MC-024880",
    "Pro Master Contract Type_8": "Master Licensing Agreement",
    "Con Application Name_2": "MC-024879"
  },
  {
    "Con Master Contract Title_3": "aaaa",
    "Con Master Contract Type_4": "Master Licensing Agreement",
    "Pro Master Contract Title_7": "Master Contract Title - MC-024882",
    "Consumer ID_5": "10629264",
    "Level One ID_1": "10629243",
    "Pro Application Name_6": "MC-024882",
    "Pro Master Contract Type_8": "Master Licensing Agreement",
    "Con Application Name_2": "MC-024879"
  },
  {
    "Con Master Contract Title_3": "aaaa",
    "Con Master Contract Type_4": "Master Licensing Agreement",
    "Pro Master Contract Title_7": "",
    "Consumer ID_5": "10629265",
    "Level One ID_1": "10629243",
    "Pro Application Name_6": "MJFF-000000",
    "Pro Master Contract Type_8": "",
    "Con Application Name_2": "MC-024879"
  },
  {
    "Con Master Contract Title_3": "aaaa",
    "Con Master Contract Type_4": "Master Licensing Agreement",
    "Pro Master Contract Title_7": "",
    "Consumer ID_5": "10629266",
    "Level One ID_1": "10629243",
    "Pro Application Name_6": "MJFF-024892",
    "Pro Master Contract Type_8": "",
    "Con Application Name_2": "MC-024879"
  }
];

//My globalAllNestedApplications sarray in this case:
globalAllNestedApplications =
[
    {
        "records": [
            {
                "Con Master Contract Title_3": "Master Contract Title - MC-024880",
                "Con Master Contract Type_4": "Master Licensing Agreement",
                "Pro Master Contract Title_7": "Master Contract Title - MC-024880",
                "Consumer ID_5": "10629301",
                "Level One ID_1": "10629263",
                "Pro Application Name_6": "MC-000022",
                "Pro Master Contract Type_8": "Master Licensing Agreement",
                "Con Application Name_2": "MC-024880",
                "nestedApplications": {
                    "records": []
                }
            },
            {
                "Con Master Contract Title_3": "Master Contract Title - MC-024880",
                "Con Master Contract Type_4": "Master Licensing Agreement",
                "Pro Master Contract Title_7": "Master Contract Title - MC-024880",
                "Consumer ID_5": "10629302",
                "Level One ID_1": "10629263",
                "Pro Application Name_6": "MC-000033",
                "Pro Master Contract Type_8": "Master Licensing Agreement",
                "Con Application Name_2": "MC-024880",
                "nestedApplications": {
                    "records": []
                }
            }
        ]
    },
    {
        "records": [
            {
                "Con Master Contract Title_3": "Master Contract Title - MC-024882",
                "Con Master Contract Type_4": "Master Licensing Agreement",
                "Pro Master Contract Title_7": "",
                "Consumer ID_5": "10629283",
                "Level One ID_1": "10629264",
                "Pro Application Name_6": "MC-024891",
                "Pro Master Contract Type_8": "",
                "Con Application Name_2": "MC-024882",
                "nestedApplications": {
                    "records": [
                        {
                            "Con Master Contract Title_3": "",
                            "Con Master Contract Type_4": "",
                            "Pro Master Contract Title_7": "fgvdg",
                            "Consumer ID_5": "10629300",
                            "Level One ID_1": "10629283",
                            "Pro Application Name_6": "MC-000011",
                            "Pro Master Contract Type_8": "Clinical Trial Agreement",
                            "Con Application Name_2": "MC-024891",
                            "nestedApplications": {
                                "records": [
                                    {
                                        "Con Master Contract Title_3": "fgvdg",
                                        "Con Master Contract Type_4": "Clinical Trial Agreement",
                                        "Pro Master Contract Title_7": "fgvdg",
                                        "Consumer ID_5": "10629306",
                                        "Level One ID_1": "10629300",
                                        "Pro Application Name_6": "MC-000111",
                                        "Pro Master Contract Type_8": "Clinical Trial Agreement",
                                        "Con Application Name_2": "MC-000011",
                                        "nestedApplications": {
                                            "records": []
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    }
]
// My desired globalFinalArray should be in this case:
globalFinalArray = [
  {
    "text": "<b>Title:</b> MC-024880, <b>Level One ID:</b> 10629263",
    "href": "#MC-024880",
    "tags": [
      "Supplements Count - 2"
    ],
    "nodes": [
      {
        "text": "<b>Title:</b> MC-000022, <b>Level One ID:</b> 10629301",
        "href": "#MC-000022",
        "tags": [
          "Supplements Count - 2"
        ]
      },
      {
        "text": "<b>Title:</b> MC-000033, <b>Level One ID:</b> 10629302",
        "href": "#MC-000033",
        "tags": [
          "Supplements Count - 2"
        ]
      }
    ]
  },
  {
    "text": "<b>Title:</b> MC-024882, <b>Level One ID:</b> 10629264",
    "href": "#MC-024882",
    "tags": [
      "Supplements Count - 1"
    ],
    "nodes": [
      {
        "text": "<b>Title:</b> MC-024891, <b>Level One ID:</b> 10629283",
        "href": "#MC-024891",
        "tags": [
          "Supplements Count - 1"
        ],
        "nodes": [
          {
            "text": "<b>Title:</b> MC-000011, <b>Level One ID:</b> 10629300",
            "href": "#MC-000022",
            "tags": [
              "Supplements Count - 1"
            ],
            "nodes": [
              {
                "text": "<b>Title:</b> MC-000111, <b>Level One ID:</b> 10629306",
                "href": "#MC-000022",
                "tags": [
                  "Supplements Count - 1"
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "text": "<b>Title:</b> MJFF-000000, <b>Level One ID:</b> 10629265",
    "href": "#MJFF-000000",
    "tags": []
  },
  {
    "text": "<b>Title:</b> MJFF-024892, <b>Level One ID:</b> 10629266",
    "href": "#MJFF-024892",
    "tags": []
  }
]

//My Code
     function constructNodes(consumerId) {
            let globalAppNodes = globalApplications
                .filter(app => app["Consumer ID_5"] === consumerId)
                .flatMap(app => app.nodes ? constructNodesFromGlobal(app.nodes) : []);

            let nestedApps = globalAllNestedApplications.filter(nestedApp => nestedApp.records.some(record => record["Level One ID_1"] === consumerId));

            let nodes = nestedApps.flatMap(nestedApp => {
                let records = nestedApp.records.filter(record => record["Level One ID_1"] === consumerId);
                return records.map(record => ({
                    text: `<b>Title:</b> ${record["Pro Application Name_6"]}, <b>Level One ID:</b> ${record["Consumer ID_5"]}`,
                    href: '#' + record["Pro Application Name_6"],
                    tags: [`Supplements Count - ${nestedApp.records.length}`],
                    nodes: constructNodes(record["Consumer ID_5"])
                }));
            });

            return [...globalAppNodes, ...nodes];
        }

        function constructNodesFromGlobal(nodes) {
            return nodes.flatMap(node => {
                return [{
                    text: node["Pro Application Name_6"],
                    href: '#' + node["Pro Application Name_6"],
                    tags: [],
                    nodes: node.nodes ? constructNodesFromGlobal(node.nodes) : []
                }];
            });
        }


globalFinalArray = globalApplications.map(globalApp => {
                    let tags = globalAllNestedApplications
                        .filter(nestedApp => nestedApp.records.some(record => record["Level One ID_1"] === globalApp["Consumer ID_5"]))
                        .map(item => `Supplements Count - ${item.records.length}`);

                    let nodes = constructNodes(globalApp["Consumer ID_5"]);

                    // Only include nodes property if it's not empty for MC-024882
                    if (globalApp["Pro Application Name_6"] === "MC-024882" && nodes.length === 0) {
                        return {
                            text: globalApp["Pro Application Name_6"],
                            href: '#' + globalApp["Pro Application Name_6"],
                            tags: tags
                        };
                    }

                    return {
                        text: `<b>Title:</b> ${globalApp["Pro Application Name_6"]}, <b>Level One ID:</b> ${globalApp["Consumer ID_5"]}`,
                        href: '#' + globalApp["Pro Application Name_6"],
                        tags: tags,
                        ...(nodes.length > 0 && { nodes }) // Conditionally include nodes property only if nodes array is not empty
                    };
                });
 
Joined
Oct 24, 2013
Messages
43
Reaction score
0
Please note I have to construct one tree for all the objects in globalApplications. Please see my desired globalFinalArray in this case at the top. Please see my code for the globalFinalArray which is

globalFinalArray = globalApplications.map(globalApp => {......
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top