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

166
Views
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 answers
Answer question

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 Report

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 Report

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 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!