Been stuck on this for a while.
let employees = [
[
['firstName', 'Joe'],
['lastName', 'Blow'],
['age', 42],
['role', 'clerk']
],
[
['firstName', 'Mary'],
['lastName', 'Jenkins'],
['age', 36],
['role', 'manager']
]
]
To yield:
[
{firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
{firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'}
]
How do transform this exactly? I have tried triple nested for loops, map/reduce, and shift(), but I can't get it work transform exactly
Try this solution. Use Array#map to iterate over first level items. In the map
function iterate over nested array items via Array#forEach and populate your object. Then from map
return that object.
let employees = [
[
['firstName', 'Joe'],
['lastName', 'Blow'],
['age', 42],
['role', 'clerk']
],
[
['firstName', 'Mary'],
['lastName', 'Jenkins'],
['age', 36],
['role', 'manager']
]
];
const newEmp = employees.map(emp => {
const obj = {};
emp.forEach(([prop, value]) => obj[prop] = value);
return obj;
});
console.log(newEmp);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With