2

I have a very large JSON object that I need to get into a tree but I'm unsure on how to do it. I'm using VueJs with Vuetify which has Treeview built in but I don't know how to actually get my data ready for the tree.

This is my data...

enter image description here

And what I need is this (obviously haven't included all the data)...

items: [
    {
        name: "Adboom",
        children: [
            {
                name: "Jaydox LTD",
                children: [
                    {
                        name: "beautifullyyoungskin.net"
                    },
                    {
                        name: "thinbodydiet.com"
                    },
                    {
                        name: "youthfulskincare.net"
                    }
                ]
            }
        ]
    },
    {
        name: "Adult",
        children: [
            {
                name: "Occonti Ltd",
                children: [
                    {
                        name: "datinginthe.eu (3d Secure)"
                    },
                    {
                        name: "datinginthe.eu (Non-3d)"
                    },
                    {
                        name: "datinginthe.eu - ST (Non-3d)"
                    },
                    {
                        name: "datinginthe.eu ST (3d Secure)"
                    }
                ]
            }
        ]
    }
]

2
  • you can use map to Restructuring data Commented Nov 27, 2018 at 11:19
  • Could you give me a little more information on this please? Commented Nov 27, 2018 at 11:24

1 Answer 1

1

You could take an array of keys for the wanted nested grouping of an object and try to find a name property with the wanted value of the level.

If found, return this item, otherwise add this item to the children property.

var data = [{ divisionName: "Adboom", merchantName: "Jaydox LTD", entityName: "beautifullyyoungskin.net" }, { divisionName: "Adboom", merchantName: "Jaydox LTD", entityName: "thinbodydiet.com" }, { divisionName: "Adboom", merchantName: "Jaydox LTD", entityName: "youthfulskincare.net" }, { divisionName: "Adult", merchantName: "Occonti Ltd", entityName: "datinginthe.eu (3d Secure)" }, { divisionName: "Adult", merchantName: "Occonti Ltd", entityName: "datinginthe.eu (Non-3d)" }, { divisionName: "Adult", merchantName: "Occonti Ltd", entityName: "datinginthe.eu - ST (Non-3d)" }, { divisionName: "Adult", merchantName: "Occonti Ltd", entityName: "datinginthe.eu ST (3d Secure)" }],
    keys = ["divisionName", "merchantName", "entityName"],
    result = data
        .reduce((r, o) => {
            keys.reduce((t, k) => {
                var temp = (t.children = t.children || []).find(p => p.name === o[k]);
                if (!temp) {
                    t.children.push(temp = { name: o[k] });
                }
                return temp;
            }, r);
            return r;
        }, {})
        .children;

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Nina, I need your help again if you don't mind. Your code has worked perfectly thus far but my project has slightly changed and needs a new requirement. As you can see in the screenshot in my original question, a division, merchant and entity all have ID columns (divisonId, merchantId, entityId). I require each node to contain an id element that contains the corresponding id. I've been trying to do this all day but can't do it. Hope you can help.
maybe you ask a new question with an extended data set (in text form), which reflects the needed properties.
Hi Nina, I have done that here... stackoverflow.com/questions/55170369/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.