Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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++;
    }
}
share|improve this question
    
What is the value for j there? Are you sure that index exists in the numerically indexed saveImageTitles array? –  Mike Brant Jun 20 at 21:02
1  
I think you want to use saveImageTitles.push(products[objectIndex].image_titles[r]); instead of saveImageTitles = products[objectIndex].image_titles[r]; –  Patrick Q Jun 20 at 21:17
    
Patrick Q - That did it! Post an answer and ill accept it. –  user1231561 Jun 20 at 22:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.