我是Java语言的新手,请尝试将一些数据推送到数组,如下所示。

async function mailSend(callback) {
User = [];
for (let scriptData of automatedScriptData.dataList) {
    let userData = {
        email: scriptData["Business Email"],
        password: "acc0unt@123",
        name: scriptData["First Name"] + " " + scriptData["Last Name"],
        firstName:scriptData["First Name"],
        title: scriptData["Title"],
        timeZone: scriptData["Time Zone"],
        location: scriptData["Country"],
        company: scriptData["Company"]
    };


    User.push(userData.firstName);
    console.log(User);

    }

}


我的结果如下。

['Brock']
['Brock','Kristian']

但我只想
['Brock','Kristian']

谁能帮我解决这个问题。

最佳答案

这是因为您在console.log(User);循环内添加了for。将其移出for循环,您将获得所需的输出。

尝试这个:

async function mailSend(callback) {
User = [];
  for (let scriptData of automatedScriptData.dataList) {
    let userData = {
      email: scriptData["Business Email"],
      password: "acc0unt@123",
      name: scriptData["First Name"] + " " + scriptData["Last Name"],
      firstName:scriptData["First Name"],
      title: scriptData["Title"],
      timeZone: scriptData["Time Zone"],
      location: scriptData["Country"],
      company: scriptData["Company"]
    };
    User.push(userData.firstName);
  }
}
console.log(User);

09-20 10:39