在数组中存储和检索数据

时间:2016-01-15 18:51:24

标签: javascript arrays multidimensional-array associative-array

我需要你的帮助,

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

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();

2 个答案:

答案 0 :(得分:1)

尝试这些(基本上你需要考虑关联数组中的数组索引)。检查此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();

答案 1 :(得分:1)

我想我可能会扩展这个主题。如果您执行以下操作:

console.log(office_a);

您可以在网络检查器上看到以下结果(我使用的是Chrome)。我只会在此处输入相关信息来实现您想要实现的目标。

Object {name: Array[1], divisions: Array[1]}
    divisions: Array[1]
        0: Object
            division1: "Division 1 Office Name"
            division2: "Division 2 Office Name"
            division3: "Division 3 Office Name"
            mailstop: "DIV3"
    name: Array[1]
        0: Object
            mailstop: "AB"
            name: "Alpha Branch"

通过查看此内容,您可以清楚地了解浏览器如何解释您的对象。首先导致使用 JavaScript Literal Notation 来访问其内容,例如通过objectName.propertyName

console.log(office_a.name);
console.log(office_a.divisions);

这将产生上面看到的数组:

divisions
    0: Object
        division1: "Division 1 Office Name"
        division2: "Division 2 Office Name"
        division3: "Division 3 Office Name"
        mailstop: "DIV3"
name
    0: Object
        mailstop: "AB"
        name: "Alpha Branch"

从这里你可以访问每个单独对象的内容,在你的例子中是一个包含一个对象的数组。

console.log(office_a.name[0]);
console.log(office_a.divisions[0]);

正如我已经向您展示的那样,访问对象的特定内容就像在该对象中使用所需的属性一样简单。在您的情况下,您希望查看所有内容,请执行以下操作:

console.log(office_a.name[0].name); // Alpha Branch
console.log(office_a.name[0].mailstop); // B1
console.log(office_a.divisions[0].division1); // Division 1 Office Name
console.log(office_a.divisions[0].mailstop); // DIV1

TL; DR

这导致以下结果(注意我修改了test()函数):

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(myObject) {
  alert(myObject.name[0].name); // Alpha Branch
  alert(myObject.name[0].mailstop); // B1
  alert(myObject.divisions[0].division1); // Division 1 Office Name
  alert(myObject.divisions[0].mailstop); // DIV1
}

test(office_a);