【问题标题】:Bad syntax for an iteration迭代的语法错误
【发布时间】:2017-09-05 20:38:10
【问题描述】:

我正在尝试创建一个循环以迭代我的变量线程: threads: Subject<{[key: string]: Thread }> = new BehaviorSubject({});

通过尝试这个,我没有得到我想要的。事实上,我的变量 threadDictionary [key] 返回我 false 而不是返回一个 Thread 对象给我。

var threadDictionary = this.threads.asObservable().map((threadDictionary: {[key: string]: Thread}) => {
  return threadDictionary;
});

for( let key in threadDictionary ) {
  console.log("threadDictionary", threadDictionary);
  console.log("threadDictionary[key]", threadDictionary[key]);

  if(threadDictionary[key].participants[0].name.startsWith(str)) {
    return threadDictionary[key];
  }
}

感谢您的帮助...

//////编辑

searchArrayThreads: { [key: string]: Thread; }[];

searchThreads: Subject<{ [key: string]: Thread; }[]> =
new BehaviorSubject<{ [key: string]: Thread; }[]>(new Array<{ [key: string]: Thread; }>());

threadTest: Subject<Array<Thread>> = new Subject()

searchUser(): void {
  this.searchArrayThreads = [];
  let str;
  let element = document.getElementById('chat-window-input');

  if(element != null) {
    str = (element as HTMLInputElement).value;
  }
  else {
    str = null;
  }

  this.threadTest.next((<any>Object).values(this.threads.getValue()));

  const check = (threadDictionary) => {
    for (let key in threadDictionary) {
      const thread = threadDictionary[key];
      if(thread.participants[0].name.startsWith(str)) {
        return thread;
      }
    }
  };

  this.threadTest.subscribe(newThreads => {
    const r = check(newThreads);
    console.log('newThreads', newThreads);
     if (r) {
      this.searchArrayThreads.push(r);
      this.searchThreads.next(this.searchArrayThreads);
    }
    if(r) {
      console.log('found !', r);
    } else {
      console.log('not found');
    }
  });
}

当我尝试时,它不起作用。我的印象是我的 threadTest 变量不包含任何内容。 我将以前的线程变量添加到新的 threadTest 变量中,如下所示:

this.threadTest.next((<any>Object).values(this.threads.getValue()));

这是我之前订阅流的功能:

displaySearchUser(): void {
  this.searchUser().subscribe((thread: Thread) => {
    if (thread !== undefined) {
      this.searchArrayThreads.push(thread);
    }
    this.searchThreads.next(this.searchArrayThreads);
  });
}

【问题讨论】:

标签: multithreading angular loops typescript rxjs


【解决方案1】:

嗨 :) 我认为主题的概念可能有些混乱。

来自https://github.com/ReactiveX/rxjs/blob/master/doc/subject.md

什么是主题? RxJS Subject 是一种特殊类型的 Observable,它允许将值多播到多个 Observer。普通的 Observable 是单播的(每个订阅的 Observer 都拥有 Observable 的独立执行),而 Subjects 是多播的。

知道这个,还有这个:

每个主题都是可观察的。

您需要订阅它才能从 observable 中获取任何更改(和值)。应该是这样的:

const check = (ths) => {
  for (let i in ths) {
    const thread = ths[i];
    if (thread.participants[0].name.startsWith(str)) {
      return thread;
    }
  }
};

threads.subscribe(newThreads => {
  const r = check(newThreads);
  if (r) {
    console.log('found !', r);
  }else {
    console.log('not found :(');
  }
});

你的线程变量应该是这样的:

threads: Subject<Array<Thread>> = new Subject();

还有一件事,我认为你应该只使用 Subject 而不是 BehaviorSubject,因为它会在开始时发出一个空的(除非你想在开始时发出一个初始值)。

整个例子都在一个 jsfiddle 中:https://jsfiddle.net/uLrgsr32/

【讨论】:

  • 是的,我是初学者.. 非常感谢您的帮助,但我不确定我是否理解。我发表了我的帖子。我们应该这样做吗?因为有错误...
  • 我不明白为什么我不能向变量线程添加任何东西。当我测试 onNext 时,它给我一个错误。
  • Subject 是一个同时是 Observer 和 Observable 的类。它处理数据流。所以主题不是数组,不能直接获取它的数据;您必须订阅才能获取数据。 ths 是检查函数中的参数名称,是新传入的线程数组。您不能使用onNext,因为它严格用于Subject,在使用BehaviourSubject 时您只使用next
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
  • 2015-03-28
相关资源
最近更新 更多