我有一个包含for循环的函数。在这个for循环中,我将4个不同的字符串保存到对象saveImageTitles
。在for循环的最后一项的运行中,我触发另一个包含另一个for循环的函数appendObject()
。
在appendObject();
中的for循环函数中,我想通过saveImageTitles
传递来自someOtherFunction(objectIndex].image[j], saveImageTitles[j]);
的每个字符串 - 但saveImageTitles[j]
将显示为 undefined
如何从for循环中保存我的字符串,以便我可以在第二个for循环中迭代它们?
编辑:添加了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++;
}
}