Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda