Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

each value of array is a object subproperty of the previous array value

so I have an array ['banana', 'apple', 'grape']

how can I transform into the object below?

{
   banana: {
       apple: {
           grape: { }
       }
   }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading