例如,我有类似的东西:
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))
但是[{..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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句