问题描述
我正在尝试以功能方式(使用 Ramda)完成此操作.我的 JSON 结构是这样的
[{用户名:'bob',年龄:30,标签:['工作','无聊']},{用户名:'jim',年龄:25,标签:['home', 'fun']},{用户名:'jane',年龄:30,标签:['假期','有趣']}]我正在尝试根据标签"属性中的值进行过滤,但没有成功.我能够过滤整数/字符串(年龄和用户名),但我无法弄清楚如何对嵌套数组(标签)中的值进行过滤.任何帮助将不胜感激.
有很多方法可以做到这一点.但我认为最干净的应该是:
R.filter(R.where({tags: R.includes('fun')}))
您可以在 Ramda REPL.
其他选项,特别是如果字段嵌套更深的情况是compose
(或pipe
)prop
或path
使用 contains
调用或可能利用镜头.
不过,我认为上面的答案最易读.
I'm trying to accomplish this in a functional manner (with Ramda). My JSON is structured like this
[
{username: 'bob', age: 30, tags: ['work', 'boring']},
{username: 'jim', age: 25, tags: ['home', 'fun']},
{username: 'jane', age: 30, tags: ['vacation', 'fun']}
]
and I am trying to filter based on a value in the 'tags' property, but have not been successful. I am able to filter on ints/strings (age and username), but I can't figure out how to do so with values in nested arrays (tags). Any help would be much appreciated.
There are many ways you could do this. But I think the cleanest one would be:
R.filter(R.where({tags: R.includes('fun')}))
You can see it in action in the Ramda REPL.
Other options, especially if the field is more deeply nested is to compose
(or pipe
) prop
or path
calls with contains
or possibly to take advantage of lenses.
Still, I think the answer above is most readable.
这篇关于Ramda:如何根据嵌套数组中的值进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!