以下是多维关联数组(我的意思是对象,因为多维关联数组在JavaScript中不存在)在JavaScript中的定义,
var array = {};
array['fruit'] = {};
array['fruit']['citrus'] = ['lemon', 'orange'];
在PHP等其他语言中,它可以定义为:
$array['fruit']['citrus'] = ['lemon', 'orange'];
是否有可能在JavaScript中创建这样的多维关联数组?
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对象表示法
当然,你可以像这样一次定义它:
var array = {
fruit: {
citrus: ['Lemon', 'Orange']
}
};