admin管理员组文章数量:1292244
I start off with an object like this:
const myObj = {
key1: {a: 123, b: 'some field1'},
key2: {a: 1, b: 'some field2'},
key3: {a: 123123, b: 'some field3'}
}
I convert it to an array using R.toPairs
, resulting in:
[
["key1", {"a": 123, "b": "some field1"}],
["key2", {"a": 1, "b": "some field2"}],
["key3", {"a": 123123, "b": "some field3"}]
]
Not sure now how to sort this array on the deeper key a
. I hear Ramda's lenses could help but not sure how. I'm also open to non-lens approaches too.
I start off with an object like this:
const myObj = {
key1: {a: 123, b: 'some field1'},
key2: {a: 1, b: 'some field2'},
key3: {a: 123123, b: 'some field3'}
}
I convert it to an array using R.toPairs
, resulting in:
[
["key1", {"a": 123, "b": "some field1"}],
["key2", {"a": 1, "b": "some field2"}],
["key3", {"a": 123123, "b": "some field3"}]
]
Not sure now how to sort this array on the deeper key a
. I hear Ramda's lenses could help but not sure how. I'm also open to non-lens approaches too.
1 Answer
Reset to default 12A straightforward way is to pose two props:
R.sortBy(
R.pose(
R.prop('a'), R.prop(1)
)
)
(R.toPairs(myObj))
See this recipe if you need more nesting.
本文标签: javascriptHow to sort an array of objects based on nested key using RamdaStack Overflow
版权声明:本文标题:javascript - How to sort an array of objects based on nested key using Ramda? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741550959a2384875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论