gpt4 book ai didi

javascript - 在数组中存储和检索数据

转载 作者:行者123 更新时间:2023-12-02 15:10:32 25 4
gpt4 key购买 nike

我需要你的帮助,

对于初学者来说,我在设置数组时遇到错误,如下所示。方法执行此操作然后检索其数据的正确方法是什么?如下所示?

var office_a = {    
name: [{
name: "Alpha Branch", mailstop: "AB"
}],
divisions: [{
division1: "Division 1 Office Name", mailstop: "DIV1",
division2: "Division 2 Office Name", mailstop: "DIV2",
division3: "Division 3 Office Name", mailstop: "DIV3"
}]
};

function test() {
alert(office_a[name].name); // -> "Alpha Branch"
alert(office_a[name].mailstop); // -> "B1"
alert(office_a[divisions][division1.name]); // -> "Division 1 Office Name"
alert(office_a[divisions][division1.mailstop]); // -> "DIV1"
}

test();

最佳答案

尝试这些(基本上您需要考虑关联数组内的数组索引)。检查这个fiddle

 var office_a = {    
name: [{
name: "Alpha Branch", mailstop: "AB"
}],
divisions: [{
division1: "Division 1 Office Name", mailstop: "DIV1",
division2: "Division 2 Office Name", mailstop: "DIV2",
division3: "Division 3 Office Name", mailstop: "DIV3"
}]
};
function test() {
console.log(office_a.name[0].name);
console.log(office_a.name[0].mailstop);
console.log(office_a.divisions[0].division1);
console.log(office_a.divisions[0].mailstop);
}
test();

关于javascript - 在数组中存储和检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34817806/

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