需要Ramda社区的帮助...我有一个对象数组,需要按“ chapter_id”进行排序,排序后,只需删除“ chapter_id”即可:]]
const stuff = [ { id: 1, title: "hello world", chapter_id: "4321" }, { id: 2, title: "new title", chapter_id: "21" }, { id: 3, title: "...", chapter_id: "33" }, { id: 4, title: "huh!?", chapter_id: "14" }, { id: 5, title: "From Earth", chapter_id: "11" }, { id: 6, title: "alien", chapter_id: "11" }, { id: 7, title: "Saturn", chapter_id: "11" }, { id: 8, title: "Mars:/", chapter_id: "21" }, { id: 9, title: "damn", chapter_id: "3" }, { id: 10, title: "test", chapter_id: "11" }, { id: 11, title: "ramda heeeelp", chapter_id: "31" }, { id: 12, title: "hello?", chapter_id: "21" } ]
结果,我想得到这个对象:
{ "3": [ { "id": "9", "title": "damn" } ], "11": [ { "id": "5", "title": "From Earth" }, { "id": "6", "title": "alien" }, { "id": "7", "title": "Saturn" }, { "id": "10", "title": "test" } ], "14": [ { "id": "4", "title": "huh!?" } ], "21": [ { "id": "2", "title": "new title" }, { "id": "8", "title": "Mars:/" }, { "id": "12", "title": "hello?" } ], "31": [ { "id": "11", "title": "ramda heeeelp" } ], "33": [ { "id": "3", "title": "..." } ], "4321": [ { "id": "1", "title": "hello world" } ] }
我如何为此苦苦挣扎:
let object = {}; map(({ chapter_id }) => { const composed = compose( map(evolve({ id: toString })), //here id is converted to a string filter(c => c.chapter_id === chapter_id), ); object[chapter_id] = composed(stuff) }, stuff);
我的结果:
{
"3": [
{
"id": "9",
"title": "damn",
"chapter_id": "3" //Dissoc this
}
],
"11": [
{
"id": "5",
"title": "From Earth",
"chapter_id": "11" //dissoc this
},
{
"id": "6",
"title": "alien",
"chapter_id": "11" //and this
},
{
"id": "7",
"title": "Saturn",
"chapter_id": "11" //and this
},
{
"id": "10",
"title": "test",
"chapter_id": "11" //and this
}
],
"14": [
{
"id": "4",
"title": "huh!?",
"chapter_id": "14" //and this
}
],
"21": [
{
"id": "2",
"title": "new title",
"chapter_id": "21" //and this
},
{
"id": "8",
"title": "Mars:/",
"chapter_id": "21" //and this
},
{
"id": "12",
"title": "hello?",
"chapter_id": "21" //and this
}
],
"31": [
{
"id": "11",
"title": "ramda heeeelp",
"chapter_id": "31" //and this!!!!!!
}
],
"33": [
{
"id": "3",
"title": "...",
"chapter_id": "33" //and this..
}
],
"4321": [
{
"id": "1",
"title": "hello world",
"chapter_id": "4321" //and this:(
}
]
}
它可以工作,但我无法从每个对象中删除“ chapter_id”,有人知道如何解决吗? :Δ
需要Ramda社区的帮助...我有一个对象数组,需要按“ chapter_id”进行排序,排序后,只需删除“ chapter_id”即可:const stuff = [{id:1,标题:“ hello world” ,chapter_id:...
首先,没有正确描述有限任务:)您真正想要的(基于我想要得到的结果
使用Ramda,您可以按key
分组,然后映射组,并从所有对象中分离key
:
正如Ori Drori指出的,这可能是Ramda方法: