在数组 Ramda 中通过 id 查找对象

Find an object by id in array Ramda

例如我有这样的东西:

const stuff = {
  "31": [
    {
      "id": "11",
      "title": "ramda heeeelp"
    },
    {
      "id": "12",
      "title": "ramda 123"
    }
  ],
  "33": [
    {
      "id": "3",
      "title": "..."
    }
  ],
  "4321": [
    {
      "id": "1",
      "title": "hello world"
    }
  ]
}

我需要找到 ID 为 11 的对象。我是怎么做到的:

map(key => find(propEq('id', 11))(stuff[key]), keys(stuff)) 

然而 returns [{..object with id 11..}, undefined, undefined] 由于地图。好的,我们可以检查对象是否未定义,但它不是我想要的那样清楚。

获取对象的值,展开数组的数组,并使用 find 和 propEq 获取对象:

const { pipe, values, flatten, find, propEq } = R

const findById = id => pipe(
  values,
  flatten,
  find(propEq({ id }))
)

const data = {"31":[{"id":"11","title":"ramda heeeelp"},{"id":"12","title":"ramda 123"}],"33":[{"id":"3","title":"..."}],"4321":[{"id":"1","title":"hello world"}]}

const result = findById('11')(data)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>