I have the list of paths like below.
var paths = [
"ubx/ubx-producao/Editora Abril/Exame 1106/FINANCAS_INOCAVAO_STARTUP_BANCOES.mp3",
"ubx/ubx-producao/Editora Alto Astral/Previsoes signos Fevereiro 2016/aquario-fevereiro.mp3",
"ubx/ubx-producao/Editora Alto Astral/Previsoes signos Fevereiro 2016/aries-fevereiro.mp3"];
I parse to JSON with the code bellow.
let treePath = { };
paths.forEach(path => {
let levels = path.split("/");
let file = levels.pop()
let prevLevel = treePath;
let prevProp = levels.shift()
levels.forEach(prop => {
prevLevel[prevProp] = prevLevel[prevProp] || {};
prevLevel = prevLevel[prevProp];
prevProp = prop
});
prevLevel[prevProp] = (prevLevel[prevProp] || []).concat([file]);
});
I got the result shown below:
My greatest difficulty is to make the output bellow
{
"text": "ubx",
"children" : [{
"text": "ubx-producao",
"children": [{
"text": "Editora Abril",
"children": [{
"text": "Exame 1106",
"children": [
"BRASIL ECONOMIA DUPLA EXPLOSIVAE.mp3","BRASIL ECONOMIA PRONTOS PARA O JANTAR.mp3"]
]
}]
},
{
"text": "Editora Alto Astral",
"children": [{
"text": "Previsoes signos Fevereiro 2016",
"children": ["aquario-fevereiro.mp3", "aquario-fevereiro.mp3.sfk"]
}]
}]
}]
}
i hope to write compact code. first, construct a tree like what you have and then traversal the tree to format the node
const paths = [
"ubx/ubx-producao/Editora Abril/Exame 1106/FINANCAS_INOCAVAO_STARTUP_BANCOES.mp3",
"ubx/ubx-producao/Editora Alto Astral/Previsoes signos Fevereiro 2016/aquario-fevereiro.mp3",
"ubx/ubx-producao/Editora Alto Astral/Previsoes signos Fevereiro 2016/aries-fevereiro.mp3"
];
// first construct a tree like what you have
const tree = paths.reduce((res, cur) => {
const arr = cur.split("/");
const strParent = arr.shift();
res[strParent] = res[strParent] || {};
let parent = res[strParent];
arr.forEach(item => {
parent[item] = parent[item] || {};
parent = parent[item];
});
return res;
}, {});
// traveral the tree to format the results
const wrap = (parent) => {
return Object.getOwnPropertyNames(parent).map(name => {
const node = {
text: name,
children: wrap(parent[name])
};
node.children = node.children.map(c => {
return c.children.length === 0 ? c.text : c;
});
return node;
});
}
console.log(JSON.stringify(wrap(tree), null, 2));
So I think I got pretty much what you asked for
First of all; the input in the thread did NOT match the expected output.
Ex. aquario-fevereiro.mp3.sfk was not present in the input.
-- Also the output was incorrectly formatted so I made some adjustments.
Here's the expected output I assumed
let expectedOutput = [{
"text": "ubx",
"children" : [
{
"text": "ubx-producao",
"children": [
{
"text": "Editora Abril",
"children": [
{
"text": "Exame 1106",
"children": ["FINANCAS_INOCAVAO_STARTUP_BANCOES.mp3"]
}
]
},
{
"text": "Editora Alto Astral",
"children": [
{
"text": "Previsoes signos Fevereiro 2016",
"children": ["aquario-fevereiro.mp3", "aries-fevereiro.mp3"]
}
]
}
]
}
]
}];
Here's the code:
let paths = [
"ubx/ubx-producao/Editora Abril/Exame 1106/FINANCAS_INOCAVAO_STARTUP_BANCOES.mp3",
"ubx/ubx-producao/Editora Alto Astral/Previsoes signos Fevereiro 2016/aquario-fevereiro.mp3",
"ubx/ubx-producao/Editora Alto Astral/Previsoes signos Fevereiro 2016/aries-fevereiro.mp3"
];
let output = convertPathsToStructure(paths);
console.log(output);
function convertPathToStructure(path) {
let finalObj = {};
let pathStr = ('' + path);
let indexSlash = pathStr.indexOf('/');
if (indexSlash > -1) {
let text = pathStr.substr(0, indexSlash);
let nextPath = pathStr.substr(indexSlash + 1);
finalObj['text'] = text;
finalObj['children'] = convertPathToStructure(nextPath);
} else {
finalObj = path;
}
return finalObj;
}
function convertPathsToStructures(paths) {
let structures = [];
paths.forEach(path => {
let output = convertPathToStructure(path);
structures.push(output);
});
return structures;
}
function convertPathsToStructure(paths) {
let treeStructures = convertPathsToStructures(paths);
let oneTreeStructure = convertTreeStructuresToStructure(treeStructures);
return oneTreeStructure;
}
function convertTreeStructuresToStructure(treeStructures) {
let isObjects = treeStructures.filter(result => result.hasOwnProperty('text')).length > 0;
if (!isObjects) {
return treeStructures;
}
let allObjs = {};
treeStructures.forEach(result => {
let {text = '', children = []} = result;
let obj = allObjs[text] || {text: '', children: []};
obj.text = text;
obj.children.push(children);
allObjs[text] = obj;
});
return Object.keys(allObjs).map(text => {
return {
text: text,
children: convertTreeStructuresToStructure(allObjs[text].children)
}
});
}
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
i have a simple jquery condition which is simply checking a css value as shown below
I'm trying to edit a table by adding rows, but running into an issue with the the partial view not being fully rendered (This is my assumption)
I have small project where I need to be able to insert/edit/delete records from JSON fileI use http://myjson
I have this annoying problem with my codeI am building a simple page and I have an image that must change while scrolling down the page