-1

I have below json response which is returned as a group by id under remote object.

However to show on UI, I need to show the data after grouping it based on name. Can someone please suggest me how to group the data based on name on the data which was already grouped by backend API.

Can someone please assist here.

current response:

{
    "remote": {
      "12345": [
        {
          "id": "642232324873ab98",
          "name": "Akon",
          "account_id": "12345",
          "account_name": "director"
        }
      ],
      "56780": [
        {
          "id": "6434343a1873ab98",
          "name": "Jim",
          "account_id": "56780",
          "account_name": "director"
          
        }
      ],
      "others": [
        {
          "id": "6421ca1873ab980001bd465a",
          "account_id": null
          "account_name": "others"

        }
      ]
    }

expected output for remote object:

"remote": {
      "director": [
        {
          "id": "6420691873ab98",
          "name": "Akon",
          "account_id": "12345",
          "account_name": "director"
        },
        {
          "id": "622121873ab98",
          "name": "Jim",
          "account_id": "56780",
          "account_name": "director"
          
        }
      ],
      "others": [
        {
          "id": "6421ca1873ab980001bd465a",
          "account_id": null,
          "account_name": "others"

        }
      ]
    }
    ```
pilchard
  • 12,414
  • 5
  • 11
  • 23
sagar verma
  • 404
  • 4
  • 10
  • Does this answer your question? [How can I group an array of objects by key?](https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key) – pilchard Apr 03 '23 at 17:39

1 Answers1

0

This is a pretty simple bit of aggregation, code is hopefully fairly self explanatory. It gets all the entries under remote and tries to aggregate them into a new array grouped by account_name or the string "other" if that is not present.

const input = {"id":"43940djkd334","topic":"local topic","start_time":"2023-03-27T16:01:16.000+00:00","duration":2223,"state":"fetched","user":{"local":[{"id":"6421ca1873ab980001bd4658","name":"Freddy","email":"freddy@testmail.io","title":null,"account_id":"62323jkds0203nsk","account_name":"testmail.io"}],"remote":{"12345":[{"id":"642232324873ab98","name":"Akon","account_id":"12345","account_name":"director"}],"56780":[{"id":"6434343a1873ab98","name":"Jim","account_id":"56780","account_name":"director"}],"others":[{"id":"6421ca1873ab980001bd465a","account_id":null}]}}};

const output = Object.entries(input.user.remote).reduce( (acc,[_,[item]]) => {
  acc[item.account_name ?? "others"] ??= [];
  acc[item.account_name ?? "others"].push(item);
  return acc;
},{})

console.log(output);
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • add a new field account_name to item if `_` is `others`? – cmgchess Apr 03 '23 at 15:09
  • just wondering, if only remote object can be group, instead of taking full object. Let me make response more clear. – sagar verma Apr 03 '23 at 15:10
  • @sagarverma So looking at your updated question, all I can see as missing is the `account_name` property from the final item. Is that all you're missing? – Jamiec Apr 03 '23 at 15:16
  • @sagarverma just `if (_ === 'others') item.account_name = 'others'` to the top should solve – cmgchess Apr 03 '23 at 15:29