【发布时间】:2021-11-19 12:32:43
【问题描述】:
假设我有这个 observables 数组:
animals$ = from([
{ animal: 'rat', color: 'grey', speed: 2 },
{ animal: 'rat', color: 'black', speed: 3 },
{ animal: 'dog', color: 'grey', speed: 2 },
{ animal: 'dog', color: 'black', speed: 1 },
{ animal: 'cat', color: 'grey', speed: 5 },
{ animal: 'cat', color: 'black', speed: 1 },
]);
我想得到一个如下格式的可观察数组,其中结果按动物类型分组,按动物字母顺序排序,颜色值转换为速度值的键:
[
{ animal: 'cat', grey: 5, black: 1 },
{ animal: 'dog', grey: 2, black: 1 },
{ animal: 'rat', grey: 1, black: 3 },
]
是否可以使用 groupBy?到目前为止,我发现的大多数示例都大不相同,结果是在数组中,而不是组合成键/值对。
【问题讨论】:
-
stackoverflow.com/questions/52793944/… 这个遮篷是不是你的问题
-
在输入
cat - grey - speed中是 2。但在输出中cat - grey - speed是 1。这是错字吗? -
@MichaelD 是的,我的错,对不起!
-
出于兴趣,是否会从重复调用 API 中检索这些数据?