I'm working on a algorithm to Transform the array into object. When the last transformed object is overwrite the previous object inside the array.
This is my code:
let array = [
[
['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']
],
[
['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']
]
];
function transformEmployeeData(array) {
let obj = {};
for(let i in array) {
for(let j in array[i]) {
obj[array[i][j][0]] = array[i][j][1];
}
array.splice(i,1,obj);
}
return array;
}
let run = transformEmployeeData(array);
I'm expected to get this result as output:
[
{firstName: "Joe", lastName: "Blow", age: 42, role: "clerk"}
{firstName: "Mary", lastName: "Jenkins", age: 36, role: "manager"}
];
Instead of getting this
[
{firstName: "Mary", lastName: "Jenkins", age: 36, role: "manager"},
{firstName: "Mary", lastName: "Jenkins", age: 36, role: "manager"}
];
When I log the the obj, I get the expected result. When I add it into an array it just overwrites the previous result.