I have a function that contains a for-loop. In this for-loop I save 4 different strings to object saveImageTitles
. At the run-through of the last item in the for loop I trigger another function appendObject()
that contains yet another for-loop.
In the for loop function in appendObject();
I want to pass on each string from saveImageTitles
via someOtherFunction(objectIndex].image[j], saveImageTitles[j]);
- but saveImageTitles[j]
will turn up as undefined.
How do I save my strings from the one for loop so that I can iterate through them in my second for-loop?
EDIT: Added a JSFiddle
var saveImageTitles = [];
var objectIndex = 0;
var objectCount = 4;
var totalObjectImages = 1;
function appendObject() {
for(var j in products[objectIndex].image) {
/* function where i want to pass on each string */
someOtherFunction(products[objectIndex].image[j], saveImageTitles[j]);
/* ISSUE - WILL ONLY PRINT OUT THE 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 to the last item in the for loop
appendObject();
}
totalObjectImages++;
}
}
saveImageTitles
array? – Mike Brant Jun 20 at 21:02saveImageTitles.push(products[objectIndex].image_titles[r]);
instead ofsaveImageTitles = products[objectIndex].image_titles[r];
– Patrick Q Jun 20 at 21:17