[英]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 的 object。我是如何做到的:
map(key => find(propEq('id', 11))(stuff[key]), keys(stuff))
然而它返回[{..object with id 11..}, undefined, undefined]
由于 map。 好的,我们可以检查 object 是否未定义,但并不清楚我想要的。
获取 object 的值,将 arrays 的数组展平,使用 find 和 propEq 得到 object:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.