7

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]);
 }
}
Alexis Tyler
9866 gold badges32 silver badges51 bronze badges
asked Apr 22, 2012 at 19:26
0

4 Answers 4

17

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;
JJJ
33.2k20 gold badges95 silver badges103 bronze badges
answered May 4, 2012 at 21:50
Sign up to request clarification or add additional context in comments.

1 Comment

How to iterate this kind of an array?
6

let concertArray = [
 ["Billy Joel", "99", "equal.png"],
 ["Bryan Adams", "89", "higher.png"],
 ["Brian Adams", "25", "lower.png"]
];
let i = 0;
console.log(concertArray[i][1]) // 99
console.log(concertArray[i][2]) // equal.png

azro
54.2k9 gold badges39 silver badges75 bronze badges
answered Apr 22, 2012 at 19:33

1 Comment

Thanks so much.... Now, what would I have to do to complete the concertImage in this structure: concertName = concertList.options[concertIndex].text; concertPrice = parseFloat(concertList.options[concertIndex].value); concertImage = ???
0

AFAIK, the Option constructor only takes two arguments, the text and the value. If you want to pass more data, I suggest you use the HTML5 data API:

var opt = new Option(concertArray[i][0], concertArray[i][1]);
opt.setAttribute('data-image', concertArray[i][2]);
select.options[select.options.length] = opt;

Then you can grab it using getAttribute('data-image')

answered Apr 22, 2012 at 19:36

Comments

0

You can access values by using 2 for loops as below.

var concertArray = [
 ["Billy Joel", "99", "equal.png"],
 ["Bryan Adams", "89", "higher.png"],
 ["Brian Adams", "25", "lower.png"]
 ];
function populate(){
 for(i=0;i<arr.length;i++)
 {
 for(j=0;j<arr[i].length;j++)
 {
 console.log(arr[i][j]);
 }
 }
}
JJJ
33.2k20 gold badges95 silver badges103 bronze badges
answered Mar 30, 2017 at 7:00

1 Comment

This answer is awfully bad for large arrays.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.