so I have an array ['banana', 'apple', 'grape']
how can I transform into the object below?
{
banana: {
apple: {
grape: { }
}
}
}
>Solution :
You can use reduceRight() to recursively build the result object:
const data = ['banana', 'apple', 'grape'];
const result = data.reduceRight((a, v) => ({ [v]: a }), {});
console.log(result);