Currently I work as a system administrator in a construction company. My goals are to expand my knowledge in computer science and gain a deep understanding of web technologies. I strive to become a competent fullstack developer. Thats why I finished a layout designer course on hexlet.io, and now I’m taking the frontend-developer course at rsschool
Description:
Implement and export by default a function that takes as input an array of a certain structure and returns an object derived from that array
Example
convert([]); // {}
convert([['key', 'value']]); // { key: 'value' }
convert([['key', 'value'], ['key2', 'value2']]); // { key: 'value', key2: 'value2' }
convert([
['key', [['key2', 'anotherValue']]],
['key2', 'value2']
]);
// { key: { key2: 'anotherValue' }, key2: 'value2' }
Code
const convert = (arr) => arr.reduce((acc, [key, value]) => {
if (!Array.isArray(value)) {
acc[key] = value;
} else {
acc[key] = convert(value);
}
return acc;
}, {});
export default convert;