Loop through multidimensional array

Hi,

I have the following array:

var movies = {
	'Movie 1' : {'year' : 1977, 'genre' : 'Sci-fi'},
	'Movie 2' : {'year' : 1990, 'genre' : 'Drama'},
	'Movie 3' : {'year' : 2003, 'genre' : 'War'},
};

I want to loop through this array and display the info on the page like:

Movie 1 - 1977, Sci-fi
Movie 2 - 1990, Drama
Movie 3 - 2003, War

I can get info about single elements using

document.write(movies['Movie 1']['year']);

but I couldn’t do the loop.Thanks for any ideas.

Curly brackets are an object, not an array. It’s square brackets that are used for an array.

For example, an array of objects:


var arr = [
    {...},
    {...},
    {...}
];

You can use a for-in loop to iterate over all the enumerable properties and methods of an object. Your object doesn’t have any methods so it would only return the properties.You’d then need to nest a second for-in loop to iterate over the individual objects that are the properties of the movies object.