Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
Modify the objects of an array of objects

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);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

console.log(jsonStudent);
const output = [];
for (var i = 0; i < jsonStudent.length; i++) {
  output.push({
    name: jsonStudent[i].Student,
    gender: jsonStudent[i].Gender,
    pet: jsonStudent[i].Pet,
    isActive: true,
    createdAt: new Date(),
    updatedAt: new Date()
  });
}
console.log(output);

Does what you're after. I've taken the liberty of changing a fair amount there, but it should be fairly self explanatory.

I think your issue was that you weren't storing the results anywhere...

Line 2 shows the creation of the output array (before the for loop starts), and then each student's data object is pushed on to the array on line 4.

Finally, once the loop is finished and closed, the output array is logged to the console.

about 4 years ago · Juan Pablo Isaza Report

0

The previous answer is perfect, but if you want a more functional approach, you can use map to iterate your array. If you do not want to modify your original array, you can use object.assign as in the example.

let newStudents = jsonStudents.map(student => {
    let newStudent = Object.assign({}, student);
    newStudent.isActive = true;
    newStudent.createdAt = new Date();
    newStudent.updatedAt = new Date();
    return newStudent;
});
console.log(newStudents);

If you do not care about modifying your original array, you can do something like this:

    let newStudents = jsonStudents.map(student => {
        student.isActive = true;
        student.createdAt = new Date();
        student.updatedAt = new Date();
        return student;
    });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!