Reputation: 10610
Here is how a multidimensional associative array (I mean object, as multidimensional associative array is not present in JavaScript) is defined generally in JavaScript,
var array = {};
array['fruit'] = {};
array['fruit']['citrus'] = ['lemon', 'orange'];
In other languages like PHP, it can be defined as,
$array['fruit']['citrus'] = ['lemon', 'orange'];
Is it possible to create a multidimensional associative array in JavaScript like this?
Upvotes: 0
Views: 2655
Reputation: 2634
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"
Also have a look at JSON - JavaScript Object Notation.
Upvotes: 3
Reputation: 97130
Sure, you can define it in one go like this:
var array = {
fruit: {
citrus: ['Lemon', 'Orange']
}
};
Upvotes: 2