Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

95
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda