what is the best way to convert an object of arrays to an array of objects and vice-versa
{
category : ['a','b','c'],
title : ['e','f','g'],
code : ['z','x','v']
}
To
[
{
category : 'a',
title : 'e',
code : 'z'
},
{
category : 'b',
title : 'f',
code : 'x'
},
{
category : 'c',
title : 'g',
code : 'v'
},
]
You can use
map()
andforEach()
You could use two function for generating an array or an object. They works with
Object.keys
for getting all own property names,Array#reduce
for iterating an array and collecting items for return,Array#forEach
just fo itarating an array.Here is solution using
reduce
method andarrow
function.