Javascript Data Structure - bootstrap treeview nodes

Viewed 93

I have a question about data structure. I want to create a array by using for loop or for each with javascript, but I have no idea how to make it.

If you have any idea about it like for-loop with example, please help me.

Here is its code. I want to make from dataArr to tree Structure by using some logic.

Thanks in advance.

====================== Input ==========================

    var dataArr =
                [
                     {"LG": "A", "MD": "A1", "SM": "A1a"},
                     {"LG": "A", "MD": "A1", "SM": "A1b"},
                     {"LG": "A", "MD": "A1", "SM": "A1c"},
                     {"LG": "A", "MD": "A2", "SM": "A2a"},
                     {"LG": "A", "MD": "A2", "SM": "A2b"},
                     {"LG": "A", "MD": "A2", "SM": "A2c"},
                     {"LG": "B", "MD": "B1", "SM": "B1a"},
                     {"LG": "B", "MD": "B1", "SM": "B1b"},
                     {"LG": "B", "MD": "B1", "SM": "B1c"},
                     {"LG": "C", "MD": "C1", "SM": "C1a"},
                     {"LG": "C", "MD": "C1", "SM": "C1b"},
                     {"LG": "C", "MD": "C1", "SM": "C1c"}
                 ]

====================== Output ==========================

    var tree = [
                {
                  text: "A",
                  nodes: [
                    {
                      text: "A1"
                      ,nodes: [
                        {
                          text: "A1a"
                        },
                        {
                          text: "A1b"
                        },
                        {
                          text: "A1c"
                        }
                       ]
                    },
                    {
                          text: "A2"
                          ,nodes: [
                            {
                              text: "A2a"
                            },
                            {
                              text: "A2b"
                            },
                            {
                              text: "A2c"
                            }
                           ]
                    }
                  ]
                 },
                 {
                  text: "B",
                  nodes: [
                    {
                      text: "B1"
                      ,nodes: [
                        {
                          text: "B1a"
                        },
                        {
                            text: "B1b"
                        },
                        {
                            text: "B1c"
                        }
                      ]
                    }
                  ]
                 },
                 {
                     text: "C",
                     nodes: [
                             {
                                 text: "C1"
                                     ,nodes: [
                                              {
                                                  text: "C1a"
                                              },
                                              {
                                                  text: "C1b"
                                              },
                                              {
                                                  text: "C1c"
                                              }
                                              ]
                             }
                             ]
                 }
                ]
0 Answers
Related