我有一个对象,它具有类别和每个类别的单词列表,如下手类型的数据库:访问对象键
var words =
{
sports: [
'baseball', 'football', 'volleyball', 'basketball', 'soccer'],
animals: [
'dog', 'cat', 'elephant', 'crocodile', 'bird'],
entertainment: [
'netflix', 'movies', 'music', 'concert', 'band', 'computer']
}
我的HTML有一个自举下拉式选单,将根据该列表显示所有类别。我的代码工作给我点击作为一个字符串的类别的值如下:
$(document).on('click', '.dropdown-menu li a', function() {
var selectedCategory;
selectedCategory = $(this).text();
//setting value of category to global variable
categorySelected = selectedCategory;
});
我需要能够找到从该值在我的数据库中的关键。 的问题是,我无法访问写着“动物” 我需要引号把我的字符串得到的话是这样的名单: words.animals
我该怎么办呢?我试过替换(),但它不起作用。
我认为你正在寻找'字[categorySelected]'? – smarx
使用单词['animals']或单词[var] –