Yacomini Yacomini - 2 months ago 5x
Javascript Question

How to define a jagged associative array in javascript?

I'm trying to define an array which works this way:

myArray[0] = 'Volkswagen'
myArray[0][0] = 'Crossfox'
myArray[1] = 'Ford'
myArray[1][0] = 'Focus'

I can do by hand but of course it's not the way to do it.
I have a simple array with this:

arrayVolks = ['Crossfox', 'Up', 'Golf'];
arrayVolks[name] = 'Volkswagen';

My problem is that I don't know how to create the first array with the index with the name of the array and then add the array.

I was thinking on some way like this:

var myArray = [ arrayVolks[name]: {arrayVolks} ]

(The code immediately over it's more like a pseudo code than actual javascript)

Is it possible?

Thank you in regards


you can't create array like this in javascript:

myArray[0] = 'Volkswagen'
myArray[0][0] = 'Crossfox'
myArray[1] = 'Ford'
myArray[1][0] = 'Focus'

how about using Object instead?

var obj = {};
obj['Volkswagen'] = ['Crossfox', 'Up', 'Golf'];
obj['Ford'] = ['Focus'];

// get all brands

// print all "Volkswagen"