以下是一般性地在JavaScript中定义多维关联数组(我的意思是对象,因为JavaScript中不存在多维关联数组),
var array = {};
array['fruit'] = {};
array['fruit']['citrus'] = ['lemon', 'orange'];
在PHP等其他语言中,可以定义为
$array['fruit']['citrus'] = ['lemon', 'orange'];
是否可以像这样在JavaScript中创建多维关联数组?
答案 0 :(得分:3)
var array = {
fruit: {
citrus: ['Lemon', 'Orange']
}
};
var fruits = array["fruit"];
>>> {"citrus": ["Lemon", "Orange"]}
var citrus_fruits = fruits["citrus"];
>>> ["Lemon", "Orange"]
var orange = citrus_fruits[1];
>>> "Orange"
另请参阅JSON - JavaScript Object Notation。
答案 1 :(得分:2)
当然,您可以像这样一次定义:
var array = {
fruit: {
citrus: ['Lemon', 'Orange']
}
};