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?

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

var array = {
    fruit: {
       citrus: ['Lemon', 'Orange']
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.