首页 > 解决方案 > 如何在循环中访问当前迭代的数组值?

问题描述

当前尝试使用具有属性的对象数组:

目标:

我想代表大约 30 个不同的人自动填写电子邮件。表单字段始终保持一致,但我填写的值将在电子邮件到电子邮件的基础上发生变化。我正在使用 TagUI 来执行此操作。

我的旧代码(下面的最后一个代码框)通过将.csv中的每一行分配给单独的数组成功填写了每个表单,但未能遍历 .csv 中特定列的值。请参阅下面最后一个代码框上方的文本以获取进一步说明。

现在我又开始了,这一次的目标是创建一个对象数组(代表正在发送的每封电子邮件)和属性(代表要在每封电子邮件中填写的每个字段)。

这是我到目前为止所得到的:

// Using TagUI for browser automation
// https://github.com/kelaberetiv/TagUI
website-to-automate-URL-here.com

// Set up the arrays to be used later
emails = []

// Load in the 'db.csv' file
// Link to .csv: https://docs.google.com/spreadsheets/d/16iF7F-8eh2eE6kDiye0GVlmOCjADQjlVE9W1KH0Y8MM/edit?usp=sharing
csv_file = 'db.csv'
load '+csv_file+' to csv_lines

// Split the string variable "lines" into an array of individual lines
lines = csv_lines.split('\n')

// Split the individual lines up into individual properties
for (i=0; i < lines.length; i++)
{
emails[i].name = properties[1].trim()
emails[i].recipients = properties[2].trim()    
properties = lines[i].split(',')



}

编辑:当我尝试以另一种方式解决此问题时,以下代码已被搁置。仍然欢迎解决方案。

我无法触发我的 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

解决方案


看起来让你的最后一个循环被跳过的唯一原因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
  }
}

推荐阅读