pedro.olimpio
pedro.olimpio

Reputation: 1498

Lodash group array by key

I have the following JSON Structure:

{
    "list":[
        {
            "id":"7ccba2c3-e6df-4cb2-94b9-2f6320366ce0",
            "name":"List 1",
            "people":[
                {"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 1","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},
                {"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 2","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},
                {"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 3","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]}
            ]
        },
        {
            "id":"a2d1d55a-e6df-4cb2-94b9-000000011212",
            "name":"List 2",
            "people":[
                {"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 4","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},
                {"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 5","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},
                {"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 6","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]}
            ]
        }

    ]
}

and I need to group people in a only one array like this:

{

  "people": [
    {"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 1","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},
    {"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 2","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},
    {"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 3","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},
    {"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 4","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},
    {"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 5","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},
    {"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 6","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]}

  ]

}

using underscore groupBy function, the return remains the same.

Anyone has any idea how can I group this content above?

Thanks in advance.

Upvotes: 0

Views: 35

Answers (1)

Ori Drori
Ori Drori

Reputation: 191976

You can use Array.flatMap() (or lodash's _.flatMap()) to flatten the list of people to as single array:

const data = {"list":[{"id":"7ccba2c3-e6df-4cb2-94b9-2f6320366ce0","name":"List 1","people":[{"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 1","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},{"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 2","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},{"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 3","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]}]},{"id":"a2d1d55a-e6df-4cb2-94b9-000000011212","name":"List 2","people":[{"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 4","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},{"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 5","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},{"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 6","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]}]}]}

const result = {
  people: data.list.flatMap(o => o.people)
}

console.log(result)

Upvotes: 1

Related Questions