Flat to Nested conversion (advanced groupBy) challenge

Hi all,

I have spent fews days trying to achieve this, but in vain, opening the challenge to everyone.

I have a denormalised result from an elastic search query, and I have to convert and display it in nested format. I’m wondering if this is possible with any wappler formatters (server or client-side)?

Here is a sample,

[
        { departmentId: "dept1", departmentName: "Finance", name: "John", role: "Analyst"},
        { departmentId: "dept1", departmentName: "Finance", name: "Kate", role: "Manager"},
        { departmentId: "dept2", departmentName: "Helpdesk", name: "Jack", role: "Assistant"},
        { departmentId: "dept2", departmentName: "Helpdesk", name: "Tom", role: "Contractor"}
]

I want to convert this into two-dimensional nested array of objects by grouping like this:

department: [
{
      department: "dept1", 
      departmentName: "Finance",
      employee [
      {
            name: "John", 
            role: "Analyst"
      },
      {
            name: "Kate", 
            role: "Manager"
      }] 
},
{
      department: "dept2", 
      departmentName: "Helpdesk",
      employee [
      {
            name: "Jack", 
            role: "Assistant"
      },
      {
            name: "Tom", 
            role: "Contractor"
      }] 
}]

Wappler pros & gurus, please give me your thoughts on this. Thanks in advance!

Achieved the above in the elastic query itself using “field collapsing” feature with elastic search, so not a wappler requirement for me - https://www.rea-group.com/blog/using-elasticsearch-field-collapsing-to-group-related-search-results/

If anyone see this thread in the future and want to achieve the above in wappler, custom formatter is the way to go! Googling “php convert flat objects into nested” gives some good results to write this custom formatter! Cheers!

Interesting approach, @patrick will check out if we can add more app connect formatters to achieve this fields collapse

1 Like