_Gia_Cat_Luong_
Senior Member
Mình nghĩ đây là bài đệ quy flatMap thôi, bạn thử tham khảo đoạn dưới đây nha:Đây là bài cá nhân bác ạ. Đề là như này nè.
Xem tệp đính kèm 689192
Em có danh sách cấp độ như thế này. GIờ yêu cầu là chuyển nó về dạng bảng như thế này
Xem tệp đính kèm 689194
Hiện tại là được giới hạn ở level 10. Nhưng trong tương lai là có thể sẽ hơn nữa.
JavaScript:
class Level {
name: string = '';
subLevels: Level[] = []
constructor(name: string, subLevels: Level[] = []) {
this.name = name
this.subLevels = subLevels
}
flatten() {
if (!this.subLevels.length) return [[this.name]]
return this.subLevels.reduce((arr, subLevel) => arr.concat(subLevel.flatten().map(detail => [this.name, ...detail])), [])
}
}
const levels = [
new Level('Level 1 - 1', [
new Level('Level 2 - 1'),
new Level('Level 2 - 2', [new Level('Level 3 - 1')]),
new Level('Level 2 - 3', [new Level('Level 3 - 2')])
]),
new Level('Level 1 - 2', [
new Level('Level 2 - 4'),
])
]
const result = levels.reduce((arr, level) => arr.concat(level.flatten()), [])
console.log(JSON.stringify(result))
. Thanks bro.
)

