For instance, i got this array:
"items": [
{
"value": "10",
"label": "LIMEIRA",
"children": []
},
{
"value": "10-3",
"label": "RECEBIMENTO",
"children": []
},
{
"value": "10-3-4",
"label": "GAVETEIRO",
"children": []
},
{
"value": "10-3-4-A1",
"label": "A1",
"children": []
},
{
"value": "10-3-4-A1-N1",
"label": "N1",
"children": []
},
{
"value": "10-3-4-A1-N1-N2",
"label": "N2",
"children": []
},
{
"value": "10-4",
"label": "MEZANINO",
"children": []
},
{
"value": "10-4-4",
"label": "GAVETEIRO",
"children": []
},
{
"value": "10-4-4-B1",
"label": "B1",
"children": []
},
{
"value": "10-3-3",
"label": "PRATELEIRA",
"children": []
},
{
"value": "10-3-3-C1",
"label": "C1",
"children": []
},
{
"value": "10-3-3-C1-N1",
"label": "N1",
"children": []
},
{
"value": "11",
"label": "N & C BRASIL PROCESSAMENTO DE DADOS LTDA",
"children": []
},
{
"value": "11-4",
"label": "MEZANINO",
"children": []
},
{
"value": "11-4-4",
"label": "GAVETEIRO",
"children": []
},
{
"value": "11-4-4-A1",
"label": "A1",
"children": []
},
{
"value": "11-4-4-A1-N2",
"label": "N2",
"children": []
},
{
"value": "11-4-4-A1-N2-N2",
"label": "N2",
"children": []
},
{
"value": "10-4-4-A1",
"label": "A1",
"children": []
},
{
"value": "10-4-4-A1-N2",
"label": "N2",
"children": []
},
{
"value": "10-4-4-A1-N2-N2",
"label": "N2",
"children": []
},
{
"value": "11-4-4-A2",
"label": "A2",
"children": []
}
]
looping through each value i should get for example:
{
"value": "10",
"label": "LIMEIRA",
"children": [
{
"value": "10-3",
"label": "RECEBIMENTO",
"children": [... it should go on]
}
]
}
i've tried some stuff with lodash by finding the latest value, but no success, tried making each one of them the "active" one and check if it matches the value and has children, if it does, do it once again, but still no success, any ideias how i could do that ?
CodePudding user response:
You could create a Map keyed by value
of all items, and establish the parent-child relationship, populating the children
property of the item that has one less element in its value
. A virtual root object keyed by a value of ""
could so collect the root objects:
const items = [{"value": "10","label": "LIMEIRA","children": []},{"value": "10-3","label": "RECEBIMENTO","children": []},{"value": "10-3-4","label": "GAVETEIRO","children": []},{"value": "10-3-4-A1","label": "A1","children": []},{"value": "10-3-4-A1-N1","label": "N1","children": []},{"value": "10-3-4-A1-N1-N2","label": "N2","children": []},{"value": "10-4","label": "MEZANINO","children": []},{"value": "10-4-4","label": "GAVETEIRO","children": []},{"value": "10-4-4-B1","label": "B1","children": []},{"value": "10-3-3","label": "PRATELEIRA","children": []},{"value": "10-3-3-C1","label": "C1","children": []},{"value": "10-3-3-C1-N1","label": "N1","children": []},{"value": "11","label": "N & C BRASIL PROCESSAMENTO DE DADOS LTDA","children": []},{"value": "11-4","label": "MEZANINO","children": []},{"value": "11-4-4","label": "GAVETEIRO","children": []},{"value": "11-4-4-A1","label": "A1","children": []},{"value": "11-4-4-A1-N2","label": "N2","children": []},{"value": "11-4-4-A1-N2-N2","label": "N2","children": []},{"value": "10-4-4-A1","label": "A1","children": []},{"value": "10-4-4-A1-N2","label": "N2","children": []},{"value": "10-4-4-A1-N2-N2","label": "N2","children": []},{"value": "11-4-4-A2","label": "A2","children": []}];
const children = [];
const map = new Map(items.map(o => [o.value, o])).set("", { children });
for (const o of items) map.get(o.value.replace(/-?[^-] $/, "")).children.push(o);
console.log(children);