I am trying to add objects in an array, each array has multiple fields like email and name. The following is how the array looks like. I want to add it to another array.
result = [
{
"email": "[email protected]",
"firstName": "abc"
},
{
"email": "[email protected]",
"firstName": "def"
}
]
The following is the logic I'm trying to apply.
var userEmail = ""
var users = [];
var newUser = {'email' : "", 'name' : "", 'type' : 'to'};
for(var i=0; i<result.length; i++){
userEmail = result[i].email
//console.log(userEmail);
newUser.email = result[i].email;
newUser.name = result[i].firstName;
users.push(newUser);
}
My expected output is this
users = [ { email: '[email protected]', name: 'abc' },
{ email: '[email protected]', name: 'def' } ]
But the output I'm getting is this
[ { email: '[email protected]', name: 'abc' },
{ email: '[email protected]', name: 'abc' } ]
Where am I going wrong?