简体   繁体   English

Ramda 从嵌套的 object 数组中获取键

[英]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.我正在尝试遍历数组并从嵌套对象中取回 id 列表。

        "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]我正在尝试返回像这样的 ID 列表 => [44, 45, 46]

I tried const result = map(path("subGroup", "id"), group), but it did not produce the result I need.我尝试const result = map(path("subGroup", "id"), group),但它没有产生我需要的结果。

The approach you've taken is fine, except that R.path expects an array of path indexes rather than multiple arguments.您采用的方法很好,除了R.path需要一组路径索引而不是多个 arguments。

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

Alternatively, you could also just use the map method of arrays to achieve the same result.或者,您也可以只使用 arrays 的map方法来获得相同的结果。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM