How would you create a deeply nested object from an array. Like so...
const a = ['a', 'b', 'c', 'd'];
to...
{
a: {
b: {
c: {
d: {}
}
}
}
}
and potentially as deep as there are elements in the array..
Use Array#reduce method.
const a = ['a', 'b', 'c', 'd'];
let res = {};
a.reduce((obj, e) => obj[e] = {}, res)
console.log(res)
Or with Array#reduceRight method.
const a = ['a', 'b', 'c', 'd'];
let res = a.reduceRight((obj, e) => ({ [e]: obj }), {})
console.log(res)
a.reduceRight((p, c) => ({ [c]: p }), {})