如何使用 Rxjs 创建统一数组?
How to create a unified array with Rxjs?
我希望从 ngrx
selectors
.
创建一个对象数组
发生的问题是它只为数组的最后一个元素而不是为每个元素创建对象数组。
我的代码:
this.userSelectorsService.usersFriends.subscribe(users => {
users.forEach(user =>
combineLatest([this.chatSelectorsService.getFriendLatestMessageById(user.id), this.chatSelectorsService.getTotalUnreadMessagesFriendById(user.id)])
.pipe(takeWhile(chatSelectors => chatSelectors[1] > 0))
.subscribe(chatSelectors => {
console.log(chatSelectors);
this.lastMessages$.next({
user,
latestMessage: {
...chatSelectors[0],
isNewLastMessage: user.isClicked,
},
});
})
);
});
结果:
我的愿望:
[
{
"id":"1",
"text":"In a world where changes are taking place quickly, the only strategy that will guarantee failure is to not take risks.",
"time":"10:20",
"isMain":false,
"isRead":false,
"isNewLastMessage":false
},
{
"id":"2",
"text":"tst",
"time":"10:20",
"isMain":false,
"isRead":false,
"isNewLastMessage":false
},
{
"id":"3",
"text":"tst 3",
"time":"10:20",
"isMain":false,
"isRead":false,
"isNewLastMessage":false
}
]
尝试构建管道,老实说,你的问题中有太多未知的东西。
this.userSelectorsService.usersFriends.pipe(
switchMap(users => {
const set = [];
for (const user of users) {
// creating a set of data per user.
set.push(combineLatest([
of(user),
this.chatSelectorsService.getFriendLatestMessageById(user.id),
this.chatSelectorsService.getTotalUnreadMessagesFriendById(user.id),
]).pipe(
takeWhile(([user, messageId, total] => total > 0),
// mapping data to the structure we need
map(([user, messageId, total]) => ({
id: user.id,
text: "tst",
time: "10:20",
isMain: false,
isRead: false,
isNewLastMessage: user.isClicked,
})),
));
}
// or combineLatest(set), or combineAll(set), depends on your needs.
return merge(...set);
}),
// toArray(), // in case of you want an array of all emits.
// forwarding data to the this.lastMessages$.
).subscribe(formattedData => this.lastMessages$.next(formattedData));
我希望从 ngrx
selectors
.
发生的问题是它只为数组的最后一个元素而不是为每个元素创建对象数组。
我的代码:
this.userSelectorsService.usersFriends.subscribe(users => {
users.forEach(user =>
combineLatest([this.chatSelectorsService.getFriendLatestMessageById(user.id), this.chatSelectorsService.getTotalUnreadMessagesFriendById(user.id)])
.pipe(takeWhile(chatSelectors => chatSelectors[1] > 0))
.subscribe(chatSelectors => {
console.log(chatSelectors);
this.lastMessages$.next({
user,
latestMessage: {
...chatSelectors[0],
isNewLastMessage: user.isClicked,
},
});
})
);
});
结果:
我的愿望:
[
{
"id":"1",
"text":"In a world where changes are taking place quickly, the only strategy that will guarantee failure is to not take risks.",
"time":"10:20",
"isMain":false,
"isRead":false,
"isNewLastMessage":false
},
{
"id":"2",
"text":"tst",
"time":"10:20",
"isMain":false,
"isRead":false,
"isNewLastMessage":false
},
{
"id":"3",
"text":"tst 3",
"time":"10:20",
"isMain":false,
"isRead":false,
"isNewLastMessage":false
}
]
尝试构建管道,老实说,你的问题中有太多未知的东西。
this.userSelectorsService.usersFriends.pipe(
switchMap(users => {
const set = [];
for (const user of users) {
// creating a set of data per user.
set.push(combineLatest([
of(user),
this.chatSelectorsService.getFriendLatestMessageById(user.id),
this.chatSelectorsService.getTotalUnreadMessagesFriendById(user.id),
]).pipe(
takeWhile(([user, messageId, total] => total > 0),
// mapping data to the structure we need
map(([user, messageId, total]) => ({
id: user.id,
text: "tst",
time: "10:20",
isMain: false,
isRead: false,
isNewLastMessage: user.isClicked,
})),
));
}
// or combineLatest(set), or combineAll(set), depends on your needs.
return merge(...set);
}),
// toArray(), // in case of you want an array of all emits.
// forwarding data to the this.lastMessages$.
).subscribe(formattedData => this.lastMessages$.next(formattedData));