使用 ramda.js 中的嵌套字段进行排序 [英] Sorting using nested field in ramda.js
本文介绍了使用 ramda.js 中的嵌套字段进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 sortBy 的文档中,它说我们可以使用 R.prop 对对象进行排序它的领域.但是,如果我必须按嵌套字段排序,则不起作用.例如 R.prop('id.number') 不起作用.
In the documentation of sortBy, it says we can use R.prop to sort an object by it's field. But if I have to sort by a nested field, it does not work. For example R.prop('id.number') does not work.
var items = [{id:3},{id:1},{id:2}];
var sorter = R.sortBy(R.prop('id'));
sorter(items)
工作正常.但是如果我有一个嵌套结构
works fine. But if I have a nested structure
var items = [{id:{number:3}},{id:{number:1}},{id:{number:2}}];
var sorter = R.sortBy(R.prop('id.number'));
sorter(items)
返回一个空列表.我想有一种使用 R.prop 的正确方法,但我无法弄清楚.
returns me an empty list. I guess there is a correct way of using R.prop that I am not able to figure out.
推荐答案
您可以使用 R.path
来访问嵌套属性,因此您的示例将变为 R.sortBy(R.path(['id', 'number']))
You can use R.path
for accessing nested properties, so your example would become R.sortBy(R.path(['id', 'number']))
这篇关于使用 ramda.js 中的嵌套字段进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文