按父 ids 对象 Ramda 分组数组
Group arrays by parent ids object Ramda
需要 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",有人知道如何解决这个问题吗? :Δ
首先,有限任务没有正确描述 :) 你真正想要的(基于 我想要得到的 部分的结果)是 规范化(或重组)对象数组为一个对象,该对象以 chapter_id
作为键,值是 stuff
数组与 chapter_id
[ 的关联记录的数组=22=]
在我看来,您的解决方案很酷而且看起来更实用,但在这种特殊情况下,我可能会优先考虑简单的 reduce
函数,它更具可读性......
reduce((acc, {chapter_id, ...rest}) => {
const isInitialized = !!acc[chapter_id];
if (isInitialized) {
acc[chapter_id].push(rest);
} else {
acc[chapter_id] = [rest];
}
return acc;
}, {}, stuff);
使用 Ramda,您可以按 key
分组,然后映射组,并从所有对象中分离 key
:
const { pipe, groupBy, prop, map, dissoc } = R;
const fn = key => pipe(
groupBy(prop(key)), // group by the key
map(map(dissoc(key))) // remove the key from all objects in all groups
);
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"}];
const result = fn('chapter_id')(stuff);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
正如 Ori Drori 所说,这可能是 Ramda 方法:
const transform = pipe (
groupBy(prop('chapter_id')),
map(map(dissoc('chapter_id'))),
)
但是,如果您确实需要对键进行排序,正如您的回答所暗示的那样,那么它需要更多的处理。您可以尝试这样的操作:
const transform = pipe (
groupBy(prop('chapter_id')),
map(map(dissoc('chapter_id'))),
toPairs,
sortBy(pipe(head, Number)),
fromPairs
)
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"}]
console.log (
transform (stuff)
)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
<script> const {pipe, groupBy, prop, map, dissoc, toPairs, sortBy, head, fromPairs} = R </script>
排序行可以用多种方式编写。也许
sort(lift(subtract)(head, head)),
或者只是
sort(([a], [b]) => a - b),
显然,如果您愿意,可以像这样提取 sortByKeys
函数:
const sortByKeys = (fn) => pipe(toPairs, sortBy(fn), fromPairs)
sortByKeys
不太可能包含在 Ramda 中,它更喜欢将对象视为名称-值对的无序集合。但它可以很容易地进入你自己的帮助库。
需要 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",有人知道如何解决这个问题吗? :Δ
首先,有限任务没有正确描述 :) 你真正想要的(基于 我想要得到的 部分的结果)是 规范化(或重组)对象数组为一个对象,该对象以 chapter_id
作为键,值是 stuff
数组与 chapter_id
[ 的关联记录的数组=22=]
在我看来,您的解决方案很酷而且看起来更实用,但在这种特殊情况下,我可能会优先考虑简单的 reduce
函数,它更具可读性......
reduce((acc, {chapter_id, ...rest}) => {
const isInitialized = !!acc[chapter_id];
if (isInitialized) {
acc[chapter_id].push(rest);
} else {
acc[chapter_id] = [rest];
}
return acc;
}, {}, stuff);
使用 Ramda,您可以按 key
分组,然后映射组,并从所有对象中分离 key
:
const { pipe, groupBy, prop, map, dissoc } = R;
const fn = key => pipe(
groupBy(prop(key)), // group by the key
map(map(dissoc(key))) // remove the key from all objects in all groups
);
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"}];
const result = fn('chapter_id')(stuff);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
正如 Ori Drori 所说,这可能是 Ramda 方法:
const transform = pipe (
groupBy(prop('chapter_id')),
map(map(dissoc('chapter_id'))),
)
但是,如果您确实需要对键进行排序,正如您的回答所暗示的那样,那么它需要更多的处理。您可以尝试这样的操作:
const transform = pipe (
groupBy(prop('chapter_id')),
map(map(dissoc('chapter_id'))),
toPairs,
sortBy(pipe(head, Number)),
fromPairs
)
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"}]
console.log (
transform (stuff)
)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
<script> const {pipe, groupBy, prop, map, dissoc, toPairs, sortBy, head, fromPairs} = R </script>
排序行可以用多种方式编写。也许
sort(lift(subtract)(head, head)),
或者只是
sort(([a], [b]) => a - b),
显然,如果您愿意,可以像这样提取 sortByKeys
函数:
const sortByKeys = (fn) => pipe(toPairs, sortBy(fn), fromPairs)
sortByKeys
不太可能包含在 Ramda 中,它更喜欢将对象视为名称-值对的无序集合。但它可以很容易地进入你自己的帮助库。