部分镜头:按属性对对象数组进行分组,使用prop值作为键 [英] Partial Lenses: Group array of objects by property, use prop value as key
问题描述
我有一个这样的对象数组:
I have an array of objects like this:
[
{ name: "Group 1", value: "Foo" },
{ name: "Group 2", value: "Bar" },
{ name: "Group 1", value: "Baz" }
]
我想使用局部镜片库将这些组转换为按键具有相应组项目的对象,如下所示:
I'd like to use Partial Lenses library to transform these groups to keys of an object with corresponding group's items, like this:
{
"Group 1": [
{ name: "Group 1", value: "Foo" },
{ name: "Group 1", value: "Baz" }
],
"Group 2": [
{ name: "Group 2", value: "Bar" }
]
}
我当前的方法是这样的,假设我将源数据存储在名为data
的变量中:
My current approach is like this, assuming I have the source data in a variable called data
:
const grouped = L.collect([L.groupBy('name'), L.entries], data)
const setKey = [L.elems, 0]
const getName = [L.elems, 1, 0, 'name']
const correctPairs = L.disperse(setKey, L.collectTotal(getName, grouped), grouped)
L.get(L.inverse(L.keyed), correctPairs)
我不喜欢我需要使用grouped
和correctPairs
变量来保存数据,因为我可能应该能够直接在合成中进行转换.您能帮我以更有意义的方式组合相同的功能吗?
I don't like that I need to use the grouped
and correctPairs
variables to hold data, as I probably should be able to do the transformation directly in the composition. Could you help me to compose the same functionality in a more meaningful way?
这里的一个偏镜头带有以上代码的游乐场.
推荐答案
我假设目标是实际创建同构,通过该同构
将这样的数组视为数组的对象,并执行更新.像一个
的双向版本拉姆达的
R.groupBy
函数.
I assume the goal is to actually create an isomorphism through which one can
view such an array as an object of arrays and also perform updates. Like a
bidirectional version of e.g. Ramda's
R.groupBy
function.
实际上,一种方法是仅使用Ramda的
R.groupBy
来实现新的原语
使用 L.iso
进行同构.
像这样:
Indeed, one approach would be to just use Ramda's
R.groupBy
to implement a new primitive
isomorphism using L.iso
.
Something like this:
const objectBy = keyL => L.iso(
R.cond([[R.is(Array), R.groupBy(L.get(keyL))]]),
R.cond([[R.is(Object), L.collect([L.values, L.elems])]])
)
需要条件,以允许数据不符合条件的可能性
预期的类型,并在结果不是undefined
的情况下将其映射到undefined
.
The conditionals are needed to allow for the possibility that the data is not of
the expected type and to map the result to undefined
in case it isn't.
下面是基于上述Ramda的游乐场
objectBy
实施.
Here is a playground with the above Ramda based
objectBy
implementation.
仅使用当前版本的偏光镜片,一种构成类似镜片的方法
objectBy
组合器如下:
Using only the current version of Partial Lenses, one way to compose a similar
objectBy
combinator would be as follows:
const objectBy = keyL => [
L.groupBy(keyL),
L.array(L.unzipWith1(L.iso(x => [L.get(keyL, x), x], L.get(1)))),
L.inverse(L.keyed)
]
也许上面有趣的部分是转换
将数组数组转换为键数组对数组(或反之).
L.unzipWith1
检查组中的所有键是否匹配,如果不匹配,则检查该组
将被映射到undefined
并被过滤掉
L.array
.如果需要的话
可以通过使用更严格的行为
L.arrays
.
Perhaps the interesting part in the above is the middle part that converts an
array of arrays into an array of key-array pairs (or the other way around).
L.unzipWith1
checks that all the keys within a group match, and if they don't, that group
will be mapped to undefined
and filtered out by
L.array
. If desired,
it is possible to get stricter behaviour by using
L.arrays
.
下面是具有以上组成的游乐场
objectBy
实施.
Here is a playground with the above composed
objectBy
implementation.
这篇关于部分镜头:按属性对对象数组进行分组,使用prop值作为键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!