2

I am getting the Url of files in the form of array like below

enter image description here

and I want to achieve an object like this

var mainObj = [
{
    name: "Home",
    files: ["excel doc 1.xlsx", "excel doc 2.xlsx"],
    folders: [{
        name: "Procedure",
        files: ["excel doc 2.xlsx"],
        folders: []
    }],
},
{
    name: "BusinessUnits",
    files: [],
    folders:[
        {
            name:"Administration",
            files:[],
            folders:[{
                name: "AlKhorDocument",
                files: [],
                folders:[
                    {
                        name: "Album1",
                        files: [],
                        folders:[......]
                    }
                ]
            }]
        }
    ]
}

]

....... kindly let me know if you can help in it.

By the way I want to achieve like that below

enter image description here

If you can suggest better, then it would help me..

1
  • Yeah. I tried alot.. cannot share you all because I spend approx 3 days on achieving this. Commented Jan 13, 2020 at 17:41

2 Answers 2

2

By having only the path parts of the strings, you could reduce the array by reducing the folder structure and add the final file to the folder structure.

var data = ['Home/excel doc 1.xlsx', 'Home/excel doc 2.xlsx', 'Home/Procedure/excel doc', 'Home/Procedure/2.xlsx', 'BusinessUnits/Administration/AlKhorDocument/Album1/text.txt'],
    result = data.reduce((r, p) => {
        var path = p.split('/'),
            file = path.pop(),
            final = path.reduce((o, name) => {
                var temp = (o.folders = o.folders || []).find(q => q.name === name);
                if (!temp) o.folders.push(temp = { name });
                return temp;
            }, r);

        (final.files = final.files || []).push(file);
        return r;
    }, {});

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

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

1 Comment

Thank you . you saved my life. It perfectly worked :) :) :)
0

You need to perform some string parsing to split up the URL strings into different parts, gathering all information you need to create a tree-like structure.

Basically you could split all your URL strings into their sections and create the final data structure by analyzing all sub-sections of URL strings.

let urls = [
  'http://host.com/Performance/excel doc 1.xlsx',
  'http://host.com/BusinessUnits/Administration/AlKhorDocument/Album1/...',
  // ...
];

let result = [];

urls.forEach(url => {
  let relevantUrl = url.replace('http://host.com/', '');  // remove the unnecessary host name
  let sections = relevantUrl.split('/');  // get all string sections from the URL

  sections.forEach(section => {
    // check if that inner object already exists
    let innerObject = result.find(obj => obj.name === section);

    if(!innerObject) {
      // create an inner object for the section
      innerObject = {
        name: section,
        files: [],
        folders: []
      };
    }

    // add the current URL section (as object) to the result
    result.push(innerObject);
  });
});

What you still have to deal with is saving the current sub-level of your section object, which you can do either iteratively or by calling a recursive function.

Comments

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.