如何获取 Observable 数组列表中的最后一个元素?

How to Get the last element in Array list of Observable?

这个函数是从我的好友列表(onlineUserModel)中过滤掉所有消息的observable,得到一个string类型的observable; 每个朋友的lastMessage

getLastMessage(onlineUserModel : OnlineUserModel): Observable<string>{
    var messagesFriend= this.allDirectMessages$.pipe(map(x => x.filter(f=>f.fromOnlineUser.userName==onlineUserModel.userName)));
    this.lastMessage$ = czt.pipe(map(last()))
    return ...;
  }

lastMessage 是来自 DirectMessage.messageModel.content 的字符串的 Obserbable; 它是一个字符串,因为如果消息是图片或声音,我会创建一个标准消息;您已收到图片或...

lastMessage$ : Observable<string>
allDirectMessages$ : Observable<DirectMessage[]> 
 <ul
              class="user-list mt-2"
              *ngFor="let onlineusermodel of onlineUsersModel$ | async"
              (click)="
                selectChat(onlineusermodel);
                removeNotificationForUser(onlineusermodel)
              "
            >
              <li class="user-list-item">
                <div class="avatar avatar-online">
                  <img
                    [src]="onlineusermodel.profilePic"
                    class="rounded-circle"
                    alt="image"
                  />
                </div>

                <div class="users-list-body">
                  <div>
                    <h5>
                      {{ onlineusermodel.userName }}
                    </h5>
                    <p>getLastMessage(onlineUserModel)</p>
                  </div>
                  <div class="last-chat-time">
                    <small class="text-muted">05 min</small>
                    <div
                      *ngIf="{
                        count: getUserNotification(onlineusermodel) | async
                      } as data"
                    >
                      <div class="new-message-count" *ngIf="data.count > 0">
                        {{ data.count }}
                        <!-- {{ getUserNotification(onlineusermodel) | async }} -->
                      </div>
                    </div>
                  </div>
                </div>
              </li>
            </ul>

这是包含来自在线用户的 DirectMessage


import { MessageViewModel } from "./messageViewModel";
import { OnlineUserModel } from "./onlineUserModel";
export class DirectMessage {
  public fromOnlineUser: OnlineUserModel | null = {
    userName: "",
    email: "",
    firstname: "",
    profilePic: "",
    isProfileComplete: "",
    isActive: "",
  };
  public messages = "";

  public messageModel: MessageViewModel = {
    content: "",
    timestamp: "",
    from: null,
    to: null,
    avatar: "",
    attachement: "",
    fileSize: "",
    fileNameSaved: "",
  };

假设他们 this.allDirectMessages$ 按时间顺序发出所有 dms 的数组,我想你需要的只是:

getLastMessage$({ userName }: OnlineUserModel) {
  return this.allDirectMessages$.pipe(
    map<DirectMessage[], DirectMessage>((dms: DirectMessage[]) => {
      const index: number = dms.lastIndexOf(
        ({fromOnlineUser}: DirectMessage) => fromOnlineUser.userName === userName
      );
      if (index === -1) {
        return null;
      } else {
        return dms[index];
      }
    }),
    map<DirectMessage, string>(this. getMsgFromDirectMsg)
  );
}

...假设 this.allDirectMessages$ 总是完成。如果它没有完成那么返回的 observable 也应该通过管道 takeUntil(this.destroy$) 其中 destroy$ 是私有的 Subject<void> 并且有一个

的 ngOnDestroy
ngOnDestroy(): void {
  const d$ = this.destroy$;
  if (d$) {
    d$.next();
    d$.complete();
  }
}

此外,我不知道如何从 Directmessage 中获取实际的字符串,所以我不知道 this.getMsgFromDirectMsg 应该是什么样子。