Javascript 如何访问多维数组(如下图所示)?

Javascript 如何访问多维数组(如下图所示)?,javascript,arrays,Javascript,Arrays,我有一个数组,我不知道如何访问某些密钥。我试图完成的只是将数组中的某些键/值作为目标。这是我的数组的一个示例 var jobs = [ { // hunting name: 'Hunting', available: [ { name: 'Housemate', description: 'You stick around the cabin of th

我有一个数组,我不知道如何访问某些密钥。我试图完成的只是将数组中的某些键/值作为目标。这是我的数组的一个示例

var jobs = [
    {
        // hunting
        name: 'Hunting',
        available: [
            {
                name: 'Housemate',
                description: 'You stick around the cabin of the hunters. You are the lowest class of the hunters.',
                salary: 10
            },
            {
                name: 'Fetcher',
                description: 'You are the fetcher of the clan. You gather leather, resources, and skin leather.',
                salary: 15
            },
            {
                name: 'Hunter',
                description: 'You are a basic hunter of the clan. You hunt for food, meat, and leather.',
                salary: 25
            },
            {
                name: 'Elder',
                description: 'You are a elder of the clan. You are respected among many, and may ask hunters for arrons.',
                salary: 0
            }
        ],
        // construction
        name: 'Construction',
        available: [
            {
                name: 'Builder',
                description: 'You are a builder. You are the lowest class of the construction tier.',
                salary: 45

            },
            {
                name: 'Driver',
                description: 'You are a driver. You do the fetching and gathering of resources.',
                salary: 55
            },
            {
                name: 'Engineer',
                description: 'You are a engineer. You do the wiring and electrical work in the construction.',
                salary: 65
            },
            {
                name: 'Overseer',
                description: 'You are the overseer. You watch over the construction and give orders.',
                salary: 80
            }
        ],
    }
];
现在请记住,我在一个数组中有多个数组。在这里,我尝试访问Hunter工作类别、Fetcher工作和建筑工程师工资

alert(jobs.'Hunting'); // gives 'missing name after . operator' error
alert(jobs.name[0]); // gives 'name is not defined' error
alert(jobs.available.'Fetcher'); //same error as number 1
alert(jobs.available.salary[0]) // gives available is not defined error

如何访问这些变量?

您没有正确构建对象,请执行以下操作:

var jobs = {
    // New child object create only for Hunting
    hunting: {
        // hunting
        name: 'Hunting', // optional
        available: [
            {
                name: 'Housemate',
                description: 'You stick around the cabin of the hunters. You are the lowest class of the hunters.',
                salary: 10
            },
            {
                name: 'Fetcher',
                description: 'You are the fetcher of the clan. You gather leather, resources, and skin leather.',
                salary: 15
            },
            {
                name: 'Hunter',
                description: 'You are a basic hunter of the clan. You hunt for food, meat, and leather.',
                salary: 25
            },
            {
                name: 'Elder',
                description: 'You are a elder of the clan. You are respected among many, and may ask hunters for arrons.',
                salary: 0
            }
        ]
     },
     // Other section, this time for Construction
     construction : {
        // construction
        name: 'Construction', // Optional too
        available: [
            {
                name: 'Builder',
                description: 'You are a builder. You are the lowest class of the construction tier.',
                salary: 45

            },
            {
                name: 'Driver',
                description: 'You are a driver. You do the fetching and gathering of resources.',
                salary: 55
            },
            {
                name: 'Engineer',
                description: 'You are a engineer. You do the wiring and electrical work in the construction.',
                salary: 65
            },
            {
                name: 'Overseer',
                description: 'You are the overseer. You watch over the construction and give orders.',
                salary: 80
            }
        ],
    }
};
现在您可以执行以下操作:

var construction_jobs = jobs.construction.available;
如果您确实希望将阵列保持在第一个维度上,您可以这样做:

var jobs = [
    {
        // hunting
        name: 'Hunting', 
        available: [ /* objects... */ ]
     },
     {
        // construction
        name: 'Construction', 
        available: [ /* objects... */ ]
    }
];
并用于获取数据:

var job = _.findWhere(jobs, {name: 'Hunting'});

要理解,请检查此代码笔上的控制台日志显示:

如果未正确构建对象,请执行以下操作:

var jobs = {
    // New child object create only for Hunting
    hunting: {
        // hunting
        name: 'Hunting', // optional
        available: [
            {
                name: 'Housemate',
                description: 'You stick around the cabin of the hunters. You are the lowest class of the hunters.',
                salary: 10
            },
            {
                name: 'Fetcher',
                description: 'You are the fetcher of the clan. You gather leather, resources, and skin leather.',
                salary: 15
            },
            {
                name: 'Hunter',
                description: 'You are a basic hunter of the clan. You hunt for food, meat, and leather.',
                salary: 25
            },
            {
                name: 'Elder',
                description: 'You are a elder of the clan. You are respected among many, and may ask hunters for arrons.',
                salary: 0
            }
        ]
     },
     // Other section, this time for Construction
     construction : {
        // construction
        name: 'Construction', // Optional too
        available: [
            {
                name: 'Builder',
                description: 'You are a builder. You are the lowest class of the construction tier.',
                salary: 45

            },
            {
                name: 'Driver',
                description: 'You are a driver. You do the fetching and gathering of resources.',
                salary: 55
            },
            {
                name: 'Engineer',
                description: 'You are a engineer. You do the wiring and electrical work in the construction.',
                salary: 65
            },
            {
                name: 'Overseer',
                description: 'You are the overseer. You watch over the construction and give orders.',
                salary: 80
            }
        ],
    }
};
现在您可以执行以下操作:

var construction_jobs = jobs.construction.available;
如果您确实希望将阵列保持在第一个维度上,您可以这样做:

var jobs = [
    {
        // hunting
        name: 'Hunting', 
        available: [ /* objects... */ ]
     },
     {
        // construction
        name: 'Construction', 
        available: [ /* objects... */ ]
    }
];
并用于获取数据:

var job = _.findWhere(jobs, {name: 'Hunting'});

为了理解,请检查此代码笔上的控制台日志显示:

我冒昧地修复了您示例中的语法错误。正如您在评论中看到的,Hunter和Construction之间缺少闭合/打开大括号

  }]},
    // construction
            {name: 'Construction',
您需要使用索引符号来获取数组中的不同元素

这将返回猎人对象。从那里,您可以访问各个元素的名称或可用名称

console.log(jobs[0]);
console.log(jobs[0].available[0]);
console.log(jobs[0].available[0].name);
这将为您提供第一个对象的name属性的名称

console.log(jobs[0].name);
这将返回“可用”下的第一个对象

console.log(jobs[0]);
console.log(jobs[0].available[0]);
console.log(jobs[0].available[0].name);
这将从available下的第一个对象返回name属性

console.log(jobs[0]);
console.log(jobs[0].available[0]);
console.log(jobs[0].available[0].name);

我冒昧地修复了您示例中的一个语法错误。正如您在评论中看到的,Hunter和Construction之间缺少闭合/打开大括号

  }]},
    // construction
            {name: 'Construction',
您需要使用索引符号来获取数组中的不同元素

这将返回猎人对象。从那里,您可以访问各个元素的名称或可用名称

console.log(jobs[0]);
console.log(jobs[0].available[0]);
console.log(jobs[0].available[0].name);
这将为您提供第一个对象的name属性的名称

console.log(jobs[0].name);
这将返回“可用”下的第一个对象

console.log(jobs[0]);
console.log(jobs[0].available[0]);
console.log(jobs[0].available[0].name);
这将从available下的第一个对象返回name属性

console.log(jobs[0]);
console.log(jobs[0].available[0]);
console.log(jobs[0].available[0].name);
您的对象数组格式不正确 原始数组包含一个项:一个对象的名称和可用属性定义了两次

我怀疑您希望数组包含两项:两个对象,每个对象都有一个名称和可用属性

console.log(jobs[0].name);
应该是这样的:

访问数组中的项 为什么下面的例子不起作用? 不能在点表示法中使用字符串:

点后不能有字符串。您的属性名称应与object.name中的相同,但首先需要通过其索引定义要在数组[i].name中作为目标的数组中的哪个项

但即使你把它改成

…它将失败,因为没有属性名为“狩猎”的对象

方括号放错了位置:

上面的示例不起作用,因为您要在属性名称之后的方括号内传递索引,它们应该放在数组名称之后。例如:

alert(jobs[0].name);
alert(jobs[0].available[0].salary);    
按键/值访问数组中的对象 看起来您正试图通过对象某个属性的值访问该对象在数组中的

例如,在上面,您似乎想要获取其name属性的值为“狩猎”的对象,这无法直接完成

您需要创建一个函数或使用一个库来为此提供函数,例如

使用u.find按键/值获取对象的示例:

您的对象数组格式不正确 原始数组包含一个项:一个对象的名称和可用属性定义了两次

我怀疑您希望数组包含两项:两个对象,每个对象都有一个名称和可用属性

console.log(jobs[0].name);
应该是这样的:

访问数组中的项 为什么下面的例子不起作用? 不能在点表示法中使用字符串:

点后不能有字符串。您的属性名称应与object.name中的相同,但首先需要通过其索引定义要在数组[i].name中作为目标的数组中的哪个项

但即使你把它改成

…它将失败,因为没有属性名为“狩猎”的对象

方括号放错了位置:

上面的示例不起作用,因为您要在属性名称之后的方括号内传递索引,它们应该放在数组名称之后。例如:

alert(jobs[0].name);
alert(jobs[0].available[0].salary);    
按键/值访问数组中的对象 看起来您正试图通过对象某个属性的值访问该对象在数组中的

例如,在上面,您似乎想要获取其name属性的值为“狩猎”的对象,这无法直接完成

您需要创建一个函数或使用一个库来为此提供函数,例如

使用u.find通过k获取对象的示例 ey/价值:



请尝试作业[0]。名称==“搜索”。作业是一个数组,第一项是一个对象。您也不能根据属性的值进行选择。请将其作为答案发布,并解释代码是如何检查它的,因为它有点让我搞不清楚它是如何抓取的it@whipdancer我花了一点时间才注意到,但狩猎和构造是数组中不同的顶级对象。将它们用作键标识符可能没有意义,因此我认为数组是有意义的。可能是错误的,但第一个对象似乎从未关闭过,这意味着所有构造属性都将覆盖以前的值。我的格式有什么遗漏吗?我看不到第二个{和}…啊,是的!我错了;//构造行中应该有一个},{。请尝试作业[0].name===“狩猎”。作业是一个数组,第一项是一个对象。您也不能通过属性的值进行选择。请将其作为答案发布,并解释代码如何检查它,因为它有点让我搞不清楚它是如何抓取的it@whipdancer我花了一点时间才注意到,但狩猎和建筑是arra内部不同的顶级对象y、 将它们用作键标识符可能没有意义,因此我认为数组是有意义的。可能是错误的,但第一个对象似乎从未关闭过,这意味着所有的构造属性都将覆盖以前的值。我是否缺少格式中的某些内容?我没有看到第二个{和}…啊,是的!我错了;在//构造行应该有一个},{。您的“如果您绝对想要…”示例似乎有最外层的{}和[]“他倒过来了。@Arthur-我也在想类似的事情,但是,如果他这样做的话,他将失去收集工作的任何秩序,例如,狩猎、建筑等。如果他在建立一个下拉列表,并且需要有秩序的选项,那么他目前的结构将更好地支持这一点。@talemyn,关于这个问题的例子,你是这样的吗?”找到属性名称的2倍,并且在对象上可用。因此,如果执行console.logjobs[0]。您将只看到{name:'Construction',available:[]}请参见此处:您的“if you very want…”示例似乎具有最外层的{}和[]“他倒过来了。@Arthur-我也在想类似的事情,但是,如果他这样做的话,他将失去收集工作的任何秩序,例如,狩猎、建筑等。如果他在建立一个下拉列表,并且需要有秩序的选项,那么他目前的结构将更好地支持这一点。@talemyn,关于这个问题的例子,你是这样的吗?”找到属性名称的2倍,并且在对象上可用。因此,如果执行console.logjobs[0]。您将只看到{name:'Construction',available:[]}请参见此处:您知道,在作业[0]上,available属性设置2次,如果使用常规javascript,第二次将附加第一次。因此,第一次将被删除。如果使用严格的javascript,这将崩溃。因此,使用此对象,您无法检索所有data@Arthur我想这就是注释中提到的语法格式错误的数组/对象。你知道,在作业[0]上,available属性设置2次,如果使用常规javascript,第二次将附加第一次。因此,第一次将被删除。如果使用严格的javascript,这将崩溃。因此,使用此对象,您无法检索所有data@Arthur我想这就是评论中提到的语法格式错误的数组/对象。喜欢解释和解释例子。谢谢,格弗兰!喜欢解释和例子。谢谢,格弗兰!