How to convert a list of object to an object in Javascript

<p>When we use JS to request data from a (relational) database, the most common way we receive the data, is as a&nbsp;<strong>list&nbsp;</strong>of&nbsp;<strong>objects</strong>.</p> <p>Often these data has the first values repeated because for example, are the days worked by a person or the purchases made by a customer.</p> <p>In my work often i need to convert this list of object in hierarchical object, i.e. a multilevel json, or in other words a&nbsp;<em>tree</em>. After some specific conversion I decided to invest some time to build a&nbsp;<em>generic&nbsp;</em>function where the&nbsp;<em>deep</em>&nbsp;of the&nbsp;<em>tree</em>&nbsp;depends by the number of the keys passed to the function.</p> <p>Since I don&rsquo;t think I&rsquo;m the only one with such a need, I decided to share this code with you all.</p> <p>The function&nbsp;<strong>generateDictFromListOfObj&nbsp;</strong>require 3 params:<br /> - the&nbsp;<strong>data:&nbsp;</strong>list of objects to convert<br /> - the&nbsp;<strong>levelsKeys</strong>: list of keys to use to buld the&nbsp;<em>tree<br /> -&nbsp;</em>the&nbsp;<strong>valuesKeys</strong>: list of keys the are the&nbsp;<em>value</em>&nbsp;keys to use in the&nbsp;<em>leave</em>&nbsp;nodes</p> <p><a href="https://stefanogiostra-it.medium.com/how-to-convert-a-list-of-object-to-an-object-in-javascript-5da20991c167">Visit Now</a></p>