2

I want build a structure like in the image below

enter image description here

But I need some help, look how far I did (don't worry about the names, the poblem is the Array)

enter image description here

I tryied the solution that I saw in this thread to convert an array to object but, without success, like you can see in the image below.

enter image description here

MY CODE

    for (index = 0; index < result.length; index++) {
        if (result[index].Parent_Id == 0) {

            // Remove all white spaces from my Text.
            var nome = result[index].Texto.replace(/ /g, '');

            // Add a new component.
            newMenu[nome] = {
                name: result[index].Texto,
                type: "item"
            };
        }
    }

    self.tree_data.push(newMenu);
    self.treeDataSource2 = new DataSourceTree({ data: self.tree_data });

How can I do a structure like the first image ? The _data in first image is a Object.

**

Community
  • 1
  • 1
Lucas_Santos
  • 4,638
  • 17
  • 71
  • 118
  • 1
    use array.reduce for the conversion as specified in the link – Akhilesh Sharma Nov 07 '14 at 18:58
  • 2
    Where do you define `self.tree_data`? It looks to be an array, since you are doing `self.tree_data.push()`. You need to declare it an object (`self.tree_data = {};`), then do `self.tree_data['prop'] = newMenu;`. – gen_Eric Nov 07 '14 at 18:58
  • @RocketHazmat Thank you, it clarifies for me, I thought that a variable with {} will be an array also. – Lucas_Santos Nov 07 '14 at 19:16
  • `var x = [];` is an array, `var y = {};` is an object. They behave differently. – gen_Eric Nov 07 '14 at 19:35

0 Answers0