0

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?

Stranger
  • 10,332
  • 18
  • 78
  • 115
  • 1
    JSON is just a subset of JavaScript so... – CD001 Aug 21 '15 at 10:05
  • possible duplicate of [Multi-dimensional associative arrays in javascript](http://stackoverflow.com/questions/4329092/multi-dimensional-associative-arrays-in-javascript) – michelem Aug 21 '15 at 10:06

2 Answers2

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"

Also have a look at JSON - JavaScript Object Notation.

user937284
  • 2,454
  • 6
  • 25
  • 29
2

Sure, you can define it in one go like this:

var array = {
    fruit: {
       citrus: ['Lemon', 'Orange']
    }
};
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156