Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert flat array of objects into nested array of objects
    primarykey
    data
    text
    <p>Original JSON data (flat table):</p> <pre><code>[ {"id":"1","first_name":"Jason","last_name":"Martin","start_date":"1996-07-25","end_date":"2006-07-25","salary":"1234.56","city":"Toronto","description":"Programmer","department":"Finance","active":"1"}, {"id":"2","first_name":"Alison","last_name":"Mathews","start_date":"1976-03-21","end_date":"1986-02-21","salary":"6661.78","city":"Vancouver","description":"Tester","department":"Finance","active":"1"}, {"id":"3","first_name":"James","last_name":"Smith","start_date":"1978-12-12","end_date":"1990-03-15","salary":"6544.78","city":"Vancouver","description":"Tester","department":"QA","active":"1"}, {"id":"4","first_name":"Celia","last_name":"Rice","start_date":"1982-10-24","end_date":"1999-04-21","salary":"2344.78","city":"Vancouver","description":"Manager","department":"HR","active":"1"}, {"id":"5","first_name":"Robert","last_name":"Black","start_date":"1984-01-15","end_date":"1998-08-08","salary":"2334.78","city":"Vancouver","description":"Tester","department":"IT","active":"1"}, {"id":"6","first_name":"Linda","last_name":"Green","start_date":"1987-07-30","end_date":"1996-01-04","salary":"4322.78","city":"New York","description":"Tester","department":"QA","active":"1"}, {"id":"7","first_name":"David","last_name":"Larry","start_date":"1990-12-31","end_date":"1998-02-12","salary":"7897.78","city":"New York","description":"Manager","department":"HR","active":"1"} ] </code></pre> <p>I need to call the function like this:</p> <pre><code>nest(data,["city","description","department"]) </code></pre> <p>The first parameter is the entire dataset, the second is an array of columns which define the nesting level.</p> <p>Expected JSON output:</p> <pre><code>[ {key: "city", value: "Toronto", count: 1, children: [ {key: "description", value: "Programmer", count: 1, children: [ {key: "department", value: "Finance", count: 1} ] } ] }, {key: "city", value: "Vancouver", count: 2, children: [ {key: "description", value: "Tester", count: 3, children: [ {key: "department", value: "Finance", count: 1}, {key: "department", value: "QA", count: 1}, {key: "department", value: "IT", count: 1} ] }, {key: "description", value: "Manager", count: 1} ] }, {key: "city", value: "New York", count: 2, children: [ {key: "description", value: "Tester", count: 1, children: [ {key: "department", value: "QA", count: 1} ] }, {key: "description", value: "Manager", count: 1, children: [ {key: "department", value: "HR", count: 1} ] } ] } </code></pre> <p>]</p> <p>I've tried writing a few recursive functions but keep getting stuck when I have to dynamically search the tree to avoid duplication.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload