gpt4 book ai didi

javascript - 使用用户输入来警告对象内的数组元素 (javascript)

转载 作者:行者123 更新时间:2023-11-28 06:42:33 25 4
gpt4 key购买 nike

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

数据层次结构如下:

  1. Cuesta - 父对象
  2. 簇 - Cuesta 内的对象
  3. 划分 - 簇内的对象
  4. 部门 - 部门内的数组(部门数组包含两个对象元素,每个元素都有两个属性:类(class)编号和类(class)标题)。

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

  1. 集群(周三或新加坡)
  2. 分区(WED 为 BUS_ED,MAS 为 MATH,等等)
  3. 部门(BUS_ED 为 CIS,ENGR_TECH 为 WELD 等)

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

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

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

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',
}
}

关于javascript - 使用用户输入来警告对象内的数组元素 (javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33664082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com