0

I'm new for javascript and try to push some data to the array as below.

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);

    }

}

My result as below.

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

But I want just [ 'Brock', 'Kristian' ]

Can anyone help me to solve that issue.

1
  • 4
    Your code works fine. Move the console.log(User) outside the loop. Commented Dec 16, 2019 at 5:26

1 Answer 1

1

This is because you have added console.log(User); inside for loop. Move this out of the for loop you will get the desired output.

Try this:

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.