我有一个数组,我不知道如何访问某些键。我想要完成的只是针对数组中的某些键/值。这是我的数组样本。
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
如何访问这些变量?
答案 0 :(得分:2)
我冒昧地修复了你的例子中的语法错误。正如你在评论中看到的那样,Hunter和Construction之间缺少紧密/开放的支撑。
}]},
// construction
{name: 'Construction',
您需要使用索引表示法来获取数组中的不同元素。
这将返回Hunter对象。从那里您可以访问各个元素(名称或可用)。
console.log(jobs[0]);
这将为您提供第一个对象的名称属性的名称。
console.log(jobs[0].name);
这将返回可用的第一个对象。
console.log(jobs[0].available[0]);
这将从可用的第一个对象返回name属性。
console.log(jobs[0].available[0].name);
答案 1 :(得分:2)
您的原始数组包含一个项目:一个对象已定义两次name
和available
属性。
我怀疑你希望你的数组包含两个项目:两个对象,每个对象都有name
和available
属性。
应该是这样的:
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
}
],
}
];
alert(jobs[0].name); // Returns 'Hunting'
alert(jobs[0].available[1].name); // Returns 'Fetcher'
alert(jobs[0].available[3].salary); // Returns '0'
您不能以点表示法使用字符串:
alert(jobs.'Hunting');
alert(jobs.available.'Fetcher');
点后面不能有字符串。您应该拥有object.name
中的属性名称,但首先需要通过其索引定义您在array[i].name
中定位的数组中的哪个项目。
但即使你把它改为......
alert(jobs[0].Hunting); // OR
alert(jobs[0]['Hunting']);
...它会失败,因为没有具有属性名称'Hunting'的对象。
方括号错位:
alert(jobs.name[0]);
alert(jobs.available.salary[0]);
上面的示例不起作用,因为您在属性名称后面的方括号内传递索引,它们应放在数组名称后面。例如:
alert(jobs[0].name);
alert(jobs[0].available[0].salary);
看起来你正试图通过其中一个属性的值来访问数组中的对象。
例如,上面似乎您想要获取name
的属性值为'Hunting'
的对象,这不能直接完成。
您需要创建一个函数或使用为此提供函数的库,例如Underscore's _.find
。
使用_.find
按键/值获取对象的示例:
var hunting = _.find(jobs, function(obj) {
return obj.name === 'Hunting';
});
答案 2 :(得分:0)
您没有正确构建对象,请执行以下操作:
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... */ ]
}
];
使用lodash lib获取数据:
var job = _.findWhere(jobs, {name: 'Hunting'});
要理解,请检查此codepen上的控制台日志显示:http://codepen.io/ArthyFiciel/pen/waGxrm