[英]using R.groupWith (ramda)
I'm trying to group the following array acc to dateValue
, but can't seem to get it to work. 我正在尝试将以下数组acc分组为dateValue
,但似乎无法使其正常工作。 What am I missing here? 我在这里想念什么?
const dates = [ {"id":83,"dateValue":"2017-05-24"}, {"id":89,"dateValue":"2017-05-28"}, {"id":91,"dateValue":"2017-05-25"}, {"id":78,"dateValue":"2017-05-24"}, {"id":84,"dateValue":"2017-05-25"} ] const groups = R.groupWith(R.eqProps('dateValue'),dates) console.log(groups)
<script src="//cdn.jsdelivr.net/ramda/latest/ramda.min.js"></script>
I've included a link to ramda repl with this code loaded 我已经包含了加载此代码的ramda repl的链接
expected result: 预期结果:
[ [ { dateValue: "2017-05-24", id: 83 },
{ dateValue: "2017-05-24", id: 78 } ],
[ { dateValue: "2017-05-28", id: 89 } ],
[ { dateValue: "2017-05-25", id: 91 } ],
[ { dateValue: "2017-05-25", id: 84 } ] ]
There are several differences between groupBy
and groupWith
. groupBy
和groupWith
之间有一些区别。
groupBy
accepts a function which yields a grouping key for a single item. groupBy
接受一个为单个项目产生分组键的函数。 groupWith
accepts a binary predicate that says whether two items match. groupWith
接受一个二进制谓词,该谓词说明两个项目是否匹配。 groupBy
collects all items into single collections. groupBy
将所有项目收集到单个集合中。 groupWith
only collects matching itemss which are consecutive . groupWith
仅收集连续的匹配项。 groupBy
returns an object that maps those keys to the list of matching items. groupBy
返回一个将这些键映射到匹配项列表的对象。 groupWith
returns a list of lists of (again consecutive) matching items. groupWith
返回匹配项列表(再次连续)的列表。 It's that second point which is tripping you up. 这是第二点使您绊倒。 You probably want groupBy
: 您可能想要groupBy
:
R.groupBy(R.prop('dateValue'))(dates)
// or
R.compose(R.values, R.groupBy(R.prop('dateValue')))(dates);
You can see this in action on the Ramda REPL . 您可以在Ramda REPL上看到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.