0
var html = {
easyBB :   
['easybbtutorials','www.easybbtutorials.com','http://i76.servimg.com/u/f76/17/83/35/07/easybb10.png'],
AvacWeb:
['AvacWeb','www.avacweb.com','http://i45.servimg.com/u/f45/16/35/08/55/new_lo12.png'],
easyBB2:
['easybbtutorials','www.easybbtutorials.com','http://i76.servimg.com/u/f76/17/83/35/07/easybb10.png'],
AvacWeb2 : 
['AvacWeb','www.avacweb.com','http://i45.servimg.com/u/f45/16/35/08/55/new_lo12.png'],
easyBB3 :
['easybbtutorials','www.easybbtutorials.com','http://i76.servimg.com/u/f76/17/83/35/07/easybb10.png'],
AvacWeb3 : 
['AvacWeb','www.avacweb.com','http://i45.servimg.com/u/f45/16/35/08/55/new_lo12.png']
};
 var cont = document.getElementById('container');
  for(var key in html){
   for(var i =0;i<key.length;i++ ){
     var name= '<span class="name">'+html[key][0] +'</span>',
     link = '<span class="url"><a href="'+html[key][1]+'">'+html[key][1] +'</a></span>',
     image = '<img src="'+html[key][2]+'" title="'+html[key][0]+'" />';        
     cont.innerHTML= '<div class="wrapper">'+ name + '<br />'+image+'<br />'+link+'</div>';
      i++;
   }
 }

I am trying to iterate over the arrays in each key of the HTML object I created problem is not sure how to do this I've tried multiple ways now and I believe (since I am posting) I am doing this all wrong. I've also tried doing: html[key[i]][0] though of course I get an error of i is not defined. Any suggestions what I am doing wrong, as of right now it is only posting one array to the html.

2
  • html['easyBB'][0] or html.easyBB[0] Commented Aug 14, 2013 at 22:12
  • 1
    It looks like you just need to get rid of your inner for loop... Commented Aug 14, 2013 at 22:12

3 Answers 3

4

The problem is not the iteration, it's the line

cont.innerHTML = ...

which is replacing the content each time the loop iterates so that you only see the final item ("AvacWeb3").

Change that to

cont.innerHTML += ...

and get rid of the for (var i =0 ... loop which isn't needed. (jsfiddle)

Sign up to request clarification or add additional context in comments.

1 Comment

Ding ding ding we have a winner! Thank you haha I'm such a dope, I added the for loop after as I did have the regular for in by itself, I never thought to look at the innerHTML! I'm trying to write just pure JS I learned jQuery first so it threw me off after always using .append thanks Stuart!
0
for(var i = 0; i < html[key].length; i++){
...

Comments

0

You should do html[key][i][0].
And also you should do what Trevor said, html[key].length instead of key.length.

Make yourself easy by assigning html[key] to var currentkey for example, easier to keep track on.

Also, look into array.forEach, just for fun ;)

Comments

Your Answer

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.