mjolk
mjolk

Reputation: 31

Ramda get keys from nested object inside array

I have an array of nested objects. I am trying to iterate though the array and get back a list of ids from the nested objects.

        "group": [
        {
          "groupId": "1",
          "subGroup": {
            "id": "44",
            "name": "Testing",
          }
        },
 {
          "groupId": "2",
          "subGroup": {
            "id": "45",
            "name": "Testing",
          }
        },
 {
          "groupId": "3",
          "subGroup": {
            "id": "46",
            "name": "Testing",
          }
        }
      ]

I am trying to return a list of ids like so => [44, 45, 46]

I tried const result = map(path("subGroup", "id"), group), but it did not produce the result I need.

Upvotes: 1

Views: 184

Answers (1)

Scott Christopher
Scott Christopher

Reputation: 6516

The approach you've taken is fine, except that R.path expects an array of path indexes rather than multiple arguments.

map(path(["subGroup", "id"]), group)

Alternatively, you could also just use the map method of arrays to achieve the same result.

group.map(g => g.subGroup.id)

Upvotes: 2

Related Questions