RuSaG0
RuSaG0

Reputation: 43

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: []
        }]
    }]
}

Upvotes: 1

Views: 311

Answers (1)

trincot
trincot

Reputation: 351029

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));

Upvotes: 1

Related Questions