如何在循环中访问当前迭代的数组值?

问题描述 投票:1回答:1

我在触发我的for循环时遇到麻烦(下面代码中的最后一个)。

用简单的英语来说,我所讨论的for循环的目标如下:重复以下代码X次,其中X由total_images数组的当前迭代确定。

因此,如果total_images数组看起来像这样:

[Total Images, 2, 3, 4, 5]

并且父级for循环处于其第三次迭代,那么此for循环应指示以下代码被执行4次。

我正在使用TagUI(https://github.com/kelaberetiv/TagUI),所以这里有很多非JavaScript代码。

https://www.website.com
wait 3s

// Setting up all the arrays that the .csv will load

array_campaign = []
array_subject = []
array_teaser = []
array_recipients = []
array_exclude = []

array_img1src = []
array_img1alt = []
array_img1url = []

array_img2src = []
array_img2alt = []
array_img2url = []

array_img3src = []
array_img3alt = []
array_img3url = []

array_img4src = []
array_img4alt = []
array_img4url = []

total_images = []

// Load in the 'db.csv' file
csv_file = 'db.csv'
load '+csv_file+' to lines

// Chop up the .csv data into individual pieces
// NOTE: Make sure the [#] corresponds to .csv column
// Reminder: Numbers start at 0
array_lines = lines.split('\n')
for (n=0; n<array_lines.length; n++)
{
  items = array_lines[n].split(',')
  array_campaign[n] = items[1].trim()
  array_recipients[n] = items[2].trim()
  array_exclude[n] = items[3].trim()
  array_subject[n] = items[4].trim()
  array_teaser[n] = items[5].trim()
  array_img1src[n] = items[6].trim()
  array_img1alt[n] = items[7].trim()
  array_img1url[n] = items[8].trim()
  array_img2src[n] = items[9].trim()
  array_img2alt[n] = items[10].trim()
  array_img2url[n] = items[11].trim()
  array_img3src[n] = items[12].trim()
  array_img3alt[n] = items[13].trim()
  array_img3url[n] = items[14].trim()
  array_img4src[n] = items[15].trim()
  array_img4alt[n] = items[16].trim()
  array_img4url[n] = items[17].trim()
  total_images[n] = items[18].trim()
}

for (i=1; i < array_campaign.length; i++)
{
echo "This is a campaign entry."
wait 2s
}


// This is the problem loop that's being skipped

blocks = total_images[i]
for (image_blocks=0; image_blocks < blocks; image_blocks++)
{
hover vis1_3.png
click visClone.png
}

这是我做过的最多的编码,因此,如果您能向正确的方向指点并像我是新手一样进行解释,将不胜感激。

javascript
1个回答
0
投票

[似乎让您的最后一个循环被跳过的唯一原因是total_images[i]undefined,用于循环条件。我相信那一刻i的值等于上一个循环中的array_campaign.length,这实际上超出了数组范围。

以下是一些示例代码:

const arr = [0, 1, 2];
const length = arr.length; // the length is 3, but the last index of this array is 2 (count from 0)

for (i = 0; i < length; i++) {
  console.log(i);
}

// output:
// 0
// 1
// 2

console.log(i); // i at this moment is 3, which is = arr.length and made the above loop exit

console.log(arr[i]); // => undefined, because the last index of the above array is 2, so if you reference to an un-existed element of an array, it will return undefined.

“将以下代码运行X次,其中X由total_images [i]的值确定-因此,如果我正确理解了您的问题,则可以使用嵌套循环来执行此操作:

for (i=1; i < array_campaign.length; i++)
{
  echo "This is a campaign entry."
  wait 2s

  // nested loop, the number of iteration is based on the value i of outside loop
  for (j=0; j < total_images[i]; j++) {
    // do something here
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.