JavaScript: Array as object

Last update: 04 Nov 23:20
0
Students

convert.js

Implement and export as default a function that takes an array of a certain structure and returns an object acquired from this array.

The array is made in such a way to be able to represent associative arrays. Each value inside it is an array of two elements, where the first element is the key and the second is the value. In turn, if the value is also an array, then it is considered a nested representation of an associative array. To put it differently: any array inside the initial one is always treated as data that needs to be converted into an object.

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' }

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests