函数中的迭代错误

时间:2017-08-18 13:10:41

标签: angular typescript rxjs

我有一个Observable线程流:

threads: Observable<{ [key: string]: Thread }>;

我想创建一个迭代我的集合中每个对象的函数,以查明 threadId 参数中给出的id是否已经存在。如果它已存在,则该函数必须返回 false

我的功能不起作用,但我不知道为什么。她总是把我弄错:

knowNewThread(threadId: string): boolean {
let newThread: boolean = false;
this.threads
  .map((threadDictionary: { [key: string]: Thread }) => {
    for (let key in threadDictionary) {
      if (threadDictionary[key].id === threadId) {
        newThread = true;
      }
    }
  });
return newThread;
}

编辑:

我不知道怎么做。我的灵感来自于我的函数,它允许我根据给定参数的 threadId 返回线程

getThreadFromSubscription(threadId: string): Observable<Thread> {
return this.threads
  .filter((threadDictionary: { [key: string]: Thread }) => {
    return Object.keys(threadDictionary).some((key) =>
      threadDictionary[key].id === threadId);
  })
  .map((threadDictionary: { [key: string]: Thread }) => {
    for (let key in threadDictionary) {
      if (threadDictionary[key].id === threadId)
      {
        return threadDictionary[key];
      }
    }
  }).first();
}

订阅之后我就这样做了:

this.getThreadFromSubscription(objMessage.id)
    .subscribe ((thread: Thread) => {
      objMessage.thread = thread;
});

我应该受到启发来创建我的功能还是不同?

threadService 中初始化线程变量:

this.threads = messageService.messages
  .map((messages: Message[]) => {
    const threads: { [key: string]: Thread } = {};
    messages.map((message: Message) => {
      threads[message.thread.id] = threads[message.thread.id] ||
        message.thread;

      const messagesThread: Thread = threads[message.thread.id];
      if (!messagesThread.lastMessage ||
        messagesThread.lastMessage.date < message.date) {
        messagesThread.lastMessage = message;
      }
    });
    return threads;
  });

/////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////////////////

编辑

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////

当我订阅我的knowNewThread函数时,它返回一个布尔变量列表。 外面我希望当我对这个函数进行scoucirs时,它返回一个布尔变量true或false。

这是我写的:

knowNewThread(threadId: string): Observable<boolean> {
return this.threads
  .map((threadDictionary: { [key: string]: Thread }) => {
    let newThread: boolean = false;
    for (let key in threadDictionary) {
      if (threadDictionary[key].id === threadId) {
        newThread = true;
      }
    }
    return newThread;
  }).first();
}

newThread: boolean = false;

this.knowNewThread(objMessage.id)
  .subscribe( (test: boolean) => {
    if(test === true) {
      this.newThread = true;
    }
});

当id已经知道时,我想让我的变量newThread为false,相反对于一个未知的id,我希望变量newThread为真。

1 个答案:

答案 0 :(得分:1)

这应该有效:

knowNewThread(threadId: string): Observable<boolean> {
  return this.threads
  .map((threadDictionary: { [key: string]: Thread }) => {
    let newThread: boolean = false;
    for (let key in threadDictionary) {
      if (threadDictionary[key].id === threadId) {
        newThread = true;
      }
    }
    return newThread;
  });
}

在您的代码中

return newThread;

将在

之前执行
for (let key in threadDictionary) { ...

因为Observable是异步的。