I need to write a function that converts array elements within an array into objects. Although I've figured out a way to solve the problem by using for-loop
, I'm just wondering if there's more concise way to write up the solution by using methods such as forEach
or map
.
The problem is...
var array: [
[
['firstName', 'Joe'],
['lastName', 'Blow'],
['age', 42],
['role', 'clerk']
],
[
['firstName', 'Mary'],
['lastName', 'Jenkins'],
['age', 36],
['role', 'manager']
]
];
I need to convert the above array into something like this.
[
{ firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk' },
{ firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager' }
];
The following is the code I've come up with by using a for-loop
.
function transformEmployeeData(array)
{
var output = [];
for (var i = 0; i < array.length; i++)
{
var obj = {};
for (var j = 0; j < array[i].length; j++)
{
obj[array[i][j][0]] = array[i][j][1];
}
output.push(obj);
}
return output;
}
Like I have mentioned above, it will be great if there's another way to solve the problem.
In some near future maybe you could use Object.fromEntries() . It is supported on some browsers version right now: Browser Compatibility :
var arr = [ [ ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk'] ], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager'] ] ]; console.log(arr.map(Object.fromEntries));
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
You could map new objects by mapping the properties and join all properties to a single objects.
var data = [[['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']], [['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']]], result = data.map(a => Object.assign(...a.map(([key, value]) => ({ [key]: value })))); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Using map and reduce.
var array = [ [['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager'] ] ]; var transformed = array.map(a=> a.reduce((c, p) => {c[p[0]] = p[1]; return c;},{})); console.log(transformed);
You can use map
and reduce
in tandem (map each element to the result of the reduce function, which transforms each element into a key/value pair):
var array=[[['firstName','Joe'],['lastName','Blow'],['age',42],['role','clerk']],[['firstName','Mary'],['lastName','Jenkins'],['age',36],['role','manager']]]; const result = array.map(e => e.reduce((a, [k, v]) => ((a[k] = v) || 1) && a, {})) ; console.log(result);
To achieve expected result, use below option of using map and forEach
var array = [ [['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager'] ] ]; console.log(array.map(val => { const result = {} val.forEach(v => { result[v[0]] = v[1] }) return result }))
codepen - https://codepen.io/nagasai/pen/ROWmEX
yes, you can do it in one line.
var array = [ [['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager'] ] ]; const transformed = array.map(upperArr => upperArr.reduce((acc, itemArr) => { acc[itemArr[0]] = itemArr[1]; return acc;}, {})); console.log(transformed);
Your function with map and forEach:
function transformEmployeeData(array) {
return array.map(toObject => {
const obj = {};
toObject.forEach(([key, value]) => {
obj[key] = value;
});
return obj;
});
};
var array = [ [['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager'] ] ]; function transformEmployeeData(array) { return array.map(toObject => { const obj = {}; toObject.forEach(([key, value]) => { obj[key] = value; }); return obj; }); }; console.log(transformEmployeeData(array));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.