Thursday, 15 September 2011

javascript - How to access object created in one for loop within another for loop? -



javascript - How to access object created in one for loop within another for loop? -

i have function contains for-loop. in for-loop save 4 different strings object saveimagetitles. @ run-through of lastly item in loop trigger function appendobject() contains yet for-loop.

in loop function in appendobject(); want pass on each string saveimagetitles via someotherfunction(objectindex].image[j], saveimagetitles[j]); - saveimagetitles[j] turn undefined.

how save strings 1 loop can iterate through them in sec for-loop?

edit: added jsfiddle

var saveimagetitles = []; var objectindex = 0; var objectcount = 4; var totalobjectimages = 1; function appendobject() { for(var j in products[objectindex].image) { /* function want pass on each string */ someotherfunction(products[objectindex].image[j], saveimagetitles[j]); /* issue - print out first item */ console.log(saveimagetitles[j]); } } function saveobject() { for(var r in products[objectindex].image_titles) { saveimagetitles = products[objectindex].image_titles[r]; if(totalobjectimages == objectcount) { //equals lastly item in loop appendobject(); } totalobjectimages++; } }

javascript arrays object for-loop

No comments:

Post a Comment