Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Adam Duffield
30,494 PointsPrint data from an associative array in javascript?
Hi all,
I have an associate array in javascript for e.g.
var tabs = [
0= ["Title1","randomurl1"],
1= ["Title2","randomurl2"],
2= "Title3","randomurl3"]
];
I am trying to print this array in a for loop to the div with the id 'searchBar'. I can manage to print just 1 of the 3 items in the array but not all 3. I am actually making a javascript based searchbar but the searchbar is the easy part for me. The part I can't get around is print the whole array to the screen.
Maybe a for loop isn't the best way to go I'm not sure but below is my code for printing it so far...
for (i = 0; i < tabs.length; i++) {
tabs[i][0] + "<br>";
}
If anyone could assist it would be much appreciated! This is actually for a small web application I have made at work to gather all links into one place, similar to FVD speed dial but for the workplace!
Regards,
Adam
2 Answers

Agapito Cruz
21,486 PointsHello Adam,
I don't think your tabs variable is an associative array. It is a multi-dimenstional array, in this case an array with 3 elements, where each element is an array of 2 strings. To access the 2nd element in each of those string arrays you could cycle through it like this:
for (i = 0; i < tabs.length; i++) {
tabs[i][1] + "<br>";
}
Or if you want to access all the elements in the string arrays, you would need a for loop within a for loop. Just remember to use a different indexing variable. I'm partial to i and j.
Also, I don't think you need to specify the keys when creating an array so your array could be written like this:
var tabs = [
["Title1","randomurl1"],
["Title2","randomurl2"],
["Title3","randomurl3"]
];
Note that I added an opening [ to the 3rd element that was missing in your original code.
BTW, I don't think javascript supports associative arrays, the closest thing would be an object where each property name could be considered a key with a corresponding value.
I hope this helps.
-Agapito

Adam Duffield
30,494 PointsI'm struggling with printing these to the screen or inside a div, any tips for this?
Adam Duffield
30,494 PointsAdam Duffield
30,494 PointsBy the way about the inline code in the forums, after plenty of attempts at trying to make it work including referencing the markdown sheet I still can't do it. I actually find it easier to code than follow this markdown! Sorry if it kinda looks allover the place.