skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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;
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search