如果我有一个包含许多类似于此的项目的数组:
[
["Core", "Mathematics", "Mathematics 20-4"],
["Core", "Mathematics", "Mathematics 30-1"],
["Other", "Fine Arts", "Art", "some art course"],
["Other", "Fine Arts", "Music", "some music course"],
["Other", "Forensics", "some forensics course"],
["French Immersion", "Core", "Mathématiques", "Mathématiques 30-1"]
]
结构本质上是“部门->主题->课程”。
我想动态创建一个类似于以下(或任何最有意义的)的数组(或对象)......
{
subjects: [
{
title: "Mathematics", courses: [ "Mathematics 20-4", "Mathematics 30-1" ]
},
{
title: "Mathématiques", lang: "fr", courses: [ "Mathématiques 30-1" ]
}
],
other: {
subjects: [
{
title: "Forensics", courses: [ "some forensics course" ]
},
{
title: "Fine Arts", subjects: [
{
title: "Art", courses: [ "some art course" ]
},
{
title: "Music", courses: [ "some music course" ]
}
]
}
]
}
}
“其他”部门不一定遵循“主题->课程”,而是可以有“主题->主题->课程”和“主题->课程”。也许添加一个 type="course" 和 type="subject" 可能会有所帮助,但我仍然希望它有一个层次结构。
我一直在思考如何将其动态转换为数组或对象结构。