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

165
Vistas
Moving variables in nested Loop JavaScript

function transformEmployeeData(employeeData) {
  var arr = [];
  var obj = {};
  for (var i = 0; i < employeeData.length; i++) {

    var personArr = employeeData[i];
    for (var j = 0; j < personArr.length; j++) {
      var key = personArr[j][0];
      var value = personArr[j][1];
      obj[key] = value;
    }
    arr.push(obj);
  }
  return arr;
}

input = [
  [
    ['firstName', 'Joe'],
    ['lastName', 'Blow'],
    ['age', 42],
    ['role', 'clerk']
  ],
  [
    ['firstName', 'Mary'],
    ['lastName', 'Jenkins'],
    ['age', 36],
    ['role', 'manager']
  ]
];

var getData = transformEmployeeData(input);
console.log(getData);

And I want my result to be like this:

[
    {firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
    {firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'}
]

So I moved var obj = {}; into the first for loop, and I got the right result. But I still cannot figure out why I have to move it inside the loop? what is the difference? Please help, thank you!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

If you don't put it in the loop, you are defining a single object and are just modifying its contents within the loop body and pushing that single object into the array.

But, if you include it inside the loop, you create a new object each time the loop body executes (and the existing variable stops pointing to the previous object and now references the new one) and then push that new object into the array, resulting in multiple objects in your output.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you do not add the object in the first loop, you are creating a single object for the whole function execution which can override previous properties of the first loop by the second one

about 4 years ago · Juan Pablo Isaza Denunciar

0

you can use map and Object.fromEntries for that

function transformEmployeeData(employeeData) {
 return employeeData.map(Object.fromEntries)
}

input = [
  [
    ['firstName', 'Joe'],
    ['lastName', 'Blow'],
    ['age', 42],
    ['role', 'clerk']
  ],
  [
    ['firstName', 'Mary'],
    ['lastName', 'Jenkins'],
    ['age', 36],
    ['role', 'manager']
  ]
];

var getData = transformEmployeeData(input);
console.log(getData);

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