使用用户输入来提醒对象中的数组元素 (javascript)

Using user input to alert array elements within objects (javascript)

我有一个包含多层对象和数组的数据层次结构。结构如下所示:

数据层次结构是这样的:

  1. Cuesta - 父对象
  2. 集群 - Cuesta 中的对象
  3. 分区 - 集群中的对象
  4. 部门 - 部门内的数组(部门数组包含两个对象元素,每个元素都有两个属性:课程编号和课程名称)。

基本上我需要提示用户指定一个

  1. 集群(周三或新加坡)
  2. 除法(BUS_ED 用于 WED 或 MATH 用于 MAS,等等)
  3. 部门(CIS BUS_ED 或 WELD ENGR_TECH,等等)

然后我需要写入存储在最终选择的数组(其元素是单个对象)中的全部信息。

基本上我只需要知道使用字符串变量调用对象的语法即可。

我一直在寻找一个半小时的解决方案,但我不知道我错过了什么。任何帮助将不胜感激!

var clus = prompt("Enter cluster: ");  // User enters cluster
var div = prompt("Enter division: ");  // User enters division
var dep = prompt("Enter department: "); // User enters department

var Cuesta =
{
    WED: {
        BUS_ED: {
            CIS: [{
                course_number: " CIS 201",
                course_title: " Introduction to Computer Science"
            }, {
                course_number: " CIS 231",
                course_title: " Fundamentals of Computer Science"
            }],
            ECON: [{
                course_number: " ECON 101",
                course_title: " Introduction to Economics"
            }, {
                course_number: " ECON 1A",
                course_title: " Intermediary Economics"
            }]
        },
        ENGR_TECH: {
            ENGR: [{
                course_number: " ENGR 101",
                course_title: " Introduction to Engineering"
            }, {
                course_number: " ENGR 1A",
                course_title: " Intermediary Engineering"
            }],
            WELD: [{
                course_number: " WELD 101",
                course_title: " Introduction to Welding"
            }, {
                course_number: " WELD 1A",
                course_title: " Intermediary Welding"
            }]
        }
    },
    MAS: {

        PHY_SCI: {

            PHYS: [{
                course_number: " PHYS 101",
                course_title: " Introduction to Physics"
            }, {
                course_number: " PHYS 1A",
                course_title: " Intermediary Physics"
            }],

            CHEM: [{
                course_number: " CHEM 101",
                course_title: " Introduction to Chemistry"
            }, {
                course_number: " CHEM 1A",
                course_title: " Intermediary Chemistry"
            }]
        },

        MATH: {
            MATH: [{
                course_number: " MATH 101",
                course_title: " Introduction to Mathematics"
            }, {
                course_number: " MATH 1A",
                course_title: " Intermediate Mathematics"
            }]
        },

        BIO: {
            BIO: [{
                course_number: " BIO 101",
                course_title: " Introduction to Biology"
            }, {
                course_number: " BIO 1A",
                course_title: " Intermediate Biology"
            }]
        }
    }
}

var out = Cuesta. + clus. + div. + dep;
/* My attempt at taking the user input to specify a department (array) whose contents I write to the
 webpage*/

document.write("Course number:" + out.[0].course_number + "Course title:");
/*My attempt at writing the user-selected department array to the webpage*/

我会使用嵌套对象,所以有一个整体对象,此时您可以以这种格式访问它的属性 exampleObject['property'] 可以访问您的子对象,并且可以像这样访问它们的属性 exampleObject['subObject']['property'] 该设置应该允许您使用字符串输入或变量来访问不同的属性。 对象本身的设置如下所示

var exampleObject={
    subObject:{
        property:'value',
    }
}

我发现了我犯的错误。我还没有了解在对象中指定元素的点符号和括号符号之间的区别。只有括号表示法可用于使用可变字符串值来指定对象内的 属性。点符号仅适用于硬编码对对象 属性 的调用。非常感谢您的帮助!我是这个网站的新手,但让我知道如何 return 以 votes/likes/etc 的形式提供帮助。