gpt4 book ai didi

javascript - 我的 Rx-js groupJoin 有什么问题

转载 作者:行者123 更新时间:2023-12-03 08:07:41 26 4
gpt4 key购买 nike

以下代码:

const Rx = require('rx');

const gameRowObservable = Rx.Observable.fromArray([{
game_id: 1,
game_name: "game one",
}, {
game_id: 2,
game_name: "game two",
}]);
const playerRowObservable = Rx.Observable.fromArray([{
game_id: 1,
player_id: 1,
player_name: "player one",
}, {
game_id: 1,
player_id: 2,
player_name: "player two",
}, {
game_id: 2,
player_id: 3,
player_name: "player three",
}, {
game_id: 2,
player_id: 4,
player_name: "player four",
}]);

const rowObservable = gameRowObservable.
groupJoin(
playerRowObservable,
gameRow => Rx.Observable.return(gameRow.game_id),
playerRow => Rx.Observable.return(playerRow.game_id),
(gameRow, playerGroup) => playerGroup.toArray().map(playerGroup => Object.assign(gameRow, {
player_list: playerGroup
}))
).mergeAll();

rowObservable.subscribe(row => console.log(row));

产生以下结果:

{ game_id: 1, game_name: 'game one', player_list: [] }
{ game_id: 2, game_name: 'game two', player_list: [] }

我想得到这个结果:

[{
game_id: 1,
game_name: "game one",
player_list: [{
game_id: 1,
player_id: 1,
player_name: "player one",
}, {
game_id: 1,
player_id: 2,
player_name: "player two",
}]
}, {
game_id: 2,
game_name: "game two",
player_list: [{
game_id: 2,
player_id: 3,
player_name: "player three",
}, {
game_id: 2,
player_id: 4,
player_name: "player four",
}]
}]

所以我基本上希望每场比赛的玩家作为数组属性。我做错了什么?

最佳答案

我无法提供有效的解决方案,但我希望能帮助您走上正确的道路。

正如您在 groupJoin 的文档中看到的那样,第二个和第三个参数旨在确定持续时间,而不是确定分组所依据的属性值

您可以通过重叠进行有效分组,即从发出 gameRowA 开始,在发出下一个 gameRow 之前发出的任何playerRow 都与 gameRowA 分组。由于您不需要任何花哨的分组,因此您的持续时间选择器只是您的源可观察量。请参阅jsbin

const rowObservable = gameRowObservable.
groupJoin(
playerRowObservable,
gameRow => gameRowObservable,
playerRow => playerRowObservable,
(gameRow, playerGroup) => playerGroup.toArray().map(playerGroup => Object.assign(gameRow, {
player_list: playerGroup
}))
).mergeAll();

rowObservable.subscribe(row => console.log(row));

关于javascript - 我的 Rx-js groupJoin 有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34308919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com