const levels = [
{
indexId: 'A',
level: 1,
name: 'A',
parent: '0',
},
{
indexId: 'A-A1',
level: 2,
name: 'A1',
parent: 'A',
},
{
indexId: 'A-A1-A11',
level: 3,
name: 'A11',
parent: 'A-A1',
},
{
indexId: 'A-A1-A11-A111',
level: 4,
name: 'A111',
parent: 'A-A1-A11',
},
{
indexId: 'A-A1-A11-A112',
level: 4,
name: 'A112',
parent: 'A-A1-A11',
},
{
indexId: 'A-A1-A12',
level: 3,
name: 'A12',
parent: 'A-A1',
},
{
indexId: 'A-A1-A12-A121',
level: 4,
name: 'A121',
parent: 'A-A1-A12',
},
{
indexId: 'A-A1-A12-A121-A1211',
level: 5,
name: 'A1211',
parent: 'A-A1-A12-A121',
},
{
indexId: 'A-A2',
level: 2,
name: 'A2',
parent: 'A',
},
{
indexId: 'A-A2-A21',
level: 3,
name: 'A21',
parent: 'A-A2',
},
{
indexId: 'A-A2-A22',
level: 3,
name: 'A22',
parent: 'A-A2',
},
{
indexId: 'A-A3',
level: 2,
name: 'A3',
parent: 'A',
},
{
indexId: 'A-A3-A31',
level: 3,
name: 'A31',
parent: 'A-A3',
},
{
indexId: 'A-A3-A32',
level: 3,
name: 'A32',
parent: 'A-A3',
},
]
const maxLevel = levels.filter(
l => l.level === Math.max(...levels.map(l => l.level))
)
const arrayStrings = []
function stackLetter(l) {
for (let index = 1; index <= l.length; index++) {
arrayStrings.push(l.slice(0, index))
}
}
stackLetter(maxLevel[0].indexId.split('-'))
console.log(arrayStrings)
const buildTree = arrayStrings
.map(arr => arr.join('-'))
.map(s => {
return levels.filter(l => l.indexId === s)
})
console.log(buildTree)
Initialement, j'ai obtenu la branche la plus longue avec le niveau maximum.
output array = [["A"],["A-A1"],["A-A1-A12"],["A-A1-A12-A121"],["A-A1-A12-A121-A1211"]]
si je sélectionne indexId = 'A-A1-A12' > j'obtiens [["A"], ["A-A1"], ["A-A1-A12"] ] ].
mais la sortie attendue est le code de toute branche liée au noeud que j'ai sélectionné. cela peut être =
[["A"],["A-A1"],["A-A1-A12"],["A-A1-A12-A121"],["A-A1-A12-A121-A1211"]]
ou si je sélectionne 'A-A3 cela devrait donner
[["A"],["A-A3"],["A-A3-A31"]]
ou
[["A"],["A-A3"],["A-A3-A32"]]