JavaScript: How to Get the Values from a Multi-Dimensional Array?

arrays, javascript, multidimensional-array

Solution

You can try to transform the multi-dimensional array to an array of objects like this:

var concertArray = [
    {name: "Billy Joel", value: 99, image: "equal.png"},
    {name: "Bryan Adams", value: 89, image: "higher.png"},
    {name: "Brian Adams", value: 25, image: "lower.png"}
];

Then you can access the items in the array like regular objects:

var concertName = concertArray[0].name;
var concertPrice = parseFloat(concertArray[0].value);
var concertImage = concertArray[0].image;

Problem

I'm trying to get the values from a multi-dimensional array. This is what I have so far. I need the value of 99 and the image when I select the first option in the array, e.g. "Billy Joel". ``` var concertArray = [ ["Billy Joel", "99", "equal.png"], ["Bryan Adams", "89", "higher.png"], ["Brian Adams", "25", "lower.png"] ]; function populate(){ for(i = 0; i < concertArray.length; i++){ var select = document.getElementById("test"); select.options[select.options.length] = new Option(concertArray[i][0], concertArray[i][1]); } } ```

Original source