流上的迭代

时间:2017-09-04 15:39:08

标签: angular typescript for-loop

我试图迭代我的​​变量线程:

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

尽管我做了很多尝试:

return this.threads.asObservable().map((threadDictionary: {[key: string]: Thread}) => {
  for( let key in threadDictionary ) {
    console.log("key", threadDictionary[key]);
    if(threadDictionary[key].participants[0].name.startsWith(str)) {
      return threadDictionary[key];
    }
  }
});


return this.threads.map((threadDictionary: {[key: string]: Thread}) => {
.flatMap((threadDictionary: {[key: string]: Thread}) => {
    for( let key in threadDictionary ) {
      console.log("key", threadDictionary[key]);
      if(threadDictionary[key].participants[0].name.startsWith(str)) {
        return threadDictionary[key];
      }
    }
  });
});

///////////////////// 编辑 /////////////////////

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

  for( let key in threadDictionary ) {
    console.log("key", threadDictionary[key]);
    if(threadDictionary[key].participants[0].name.startsWith(str)) {
      return threadDictionary[key];
    }
  }

我想知道是否可以在我的变量线程上创建For循环。我应该改变它的变量类型吗?或者如何制作这个循环?

1 个答案:

答案 0 :(得分:0)

您不能遍历threadDictionary参数,因为它不是数组。您可以尝试从调用.map()返回值,然后迭代结果。

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

for( let key in threadDictionary ) {
  console.log("key", threadDictionary[key]);
  if(threadDictionary[key].participants[0].name.startsWith(str)) {
    return threadDictionary[key];
  }
}