My current issue with this exercise is that as I convert the first nested array into an object, the iteration continues to the next nested array and ends up overwriting the object I just created.
I'm wondering how I can instruct my code to stop iterating over the converted object and instead create a new one for each nested array.
In addition to my previous question, the exercise requires me to store these newly created objects in an array. My plan was to create the objects first and then push them into a placeholder array variable at the end. Is there a more efficient way to do this within the loop?
I would appreciate any assistance, as I am still relatively new here so please be kind!
Here is what I have done so far along with the instructions:
*I am tasked with writing a function called “transformEmployeeData” that converts employee data from one format to another.
The input will look like this:
[
[
['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']
],
[
['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']
]
]
The expected output should be:</p>
[
{firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
{firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'}
]
<p>Please note that the input may vary in rows or keys from the given sample.</p>
<p>If, for example, the HR department adds a “tshirtSize” field to each employee record, your code should be able to handle it flexibly.*</p>
<pre><code>function transformEmployeeData(employeeData) {
debugger;
var obj = {};
for (var i = 0; i < employeeData.length; i++) {
for (var y = 0; y < employeeData[i].length; y++) {
obj[employeeData[i][y][0]] = employeeData[i][y][1]
}
}
return obj
}