Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
string path to folders/file object javascript

I have some problems with serialise data.

I have array of objects like this (pseudo-files):

const files = [
{ 
  id: 'ruslan/cache/vars.json',
  size: 17 
},
{
    id: 'cache/vars.json.bak',
  size: 17 
},
{
  id: 'wcc-config.json',
  size: 10
}];

How i can convert without recursion this to tree with folders and files inside like:

rootFolder = {
    files: [{
        name: 'wcc-config.json',
        size: 10
    }],
    folders: [{
        name: 'cache',
        files: [{
            name: 'vars.json.bak',
            size: 17
        }],
        folders: []
    }, {
        name: 'ruslan',
        files: [],
        folders: [{
            name: 'cache',
            files: [{
                name: 'vars.json',
                size: 17
            }],
            folders: []
        }]
    }]
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here is a non-recursive function. It uses a helper object mapper which keeps track of the created objects by their path (as key). This is needed to find the parent of a new node, so we can insert that node in the files or folders property of that parent.

The code:

function makeTree(files) {
    const mapper = {};
    for (const {id, size} of files) {
        let node, parent = "";
        let i = 0, j = 0;
        while (j > -1) {
            let path = id.slice(0, j);
            if (!mapper[path]) {
                node = {
                    name: id.slice(i, j),
                    files: [],
                    folders: [],
                };
                mapper[path] = node;
                if (path) mapper[parent].folders.push(node);
            }
            parent = path;
            i = j + !!i;
            j = id.indexOf("/", i);
        }
        node = {
            name: id.slice(i),
            size
        };
        mapper[id] = node;
        mapper[parent].files.push(node);
    }
    return mapper[""];
}

// Example run
const files = [{ id: 'ruslan/cache/vars.json', size: 17 },{ id: 'cache/vars.json.bak',  size: 17 },{ id: 'wcc-config.json',size: 10}];
console.log(makeTree(files));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!