I want to modify the objects of an array of objects (jsonStudent).
[
{ Student: 'Riya', Gender: 'Female', Pet: 'Timmy' },
{ Student: 'Happy', Gender: 'Male', Pet: 'Harry' },
{ Student: 'Tiya', Gender: 'Female', Pet: 'Timmy' },
]
And my expected output is(input):
[
{
name: 'Riya',
gender: 'Female',
pet: 'Timmy',
isActive: true,
createdAt: 2022-06-28T17:55:53.907Z,
updatedAt: 2022-06-28T17:55:53.907Z
},
{
name: 'Happy',
gender: 'Male',
pet: 'Harry',
isActive: true,
createdAt: 2022-06-28T17:55:53.913Z,
updatedAt: 2022-06-28T17:55:53.913Z
},
{
name: 'Tiya',
gender: 'Female',
pet: 'Timmy',
isActive: true,
createdAt: 2022-06-28T17:55:53.919Z,
updatedAt: 2022-06-28T17:55:53.919Z
}
]
For that I used this lines of code, but instead of all objects I am getting only the 1st object in output,Can anyone please help me to get all the objects as my expected output.
console.log(jsonStudent);
for (var i = 0; i < jsonStudent.length; i++) {
const nameVal = jsonStudent[i].Student;
const genderVal = jsonStudent[i].Gender;
const petVal = jsonStudent[i].Pet;
const now = new Date();
const input = {
name: nameVal,
gender: genderVal,
pet: petVal,
};
input.isActive = true;
input.createdAt = now;
input.updatedAt = now;
console.log(input);