复杂阵列如果我有类似这样的很多元素的数组:创建一个从列表
[
["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”可能会有所帮助,但我仍然喜欢它有一个层次。
我一直在抨击如何动态地将其转换为数组或对象结构。
其他类别让我头疼。 – tymeJV 2013-03-11 23:37:50
听起来更像是你想让别人为你写代码而不是有特定的问题或问题。 您可以尝试创建对象的层次结构,创建对象,如:课程,课程类型,部门,主题和课程,然后将您的数组条目传递到基础对象上的addCourse函数,并解析它以决定是否将项目添加到现有对象如果部门,科目或课程不存在,则可以在层次结构中创建新的或创建新的部门。你仍然会得到你想要的树层次结构,但是你将能够将问题分解成更易于管理的块。 – 2013-03-11 23:44:50