迭代语法错误 [英] Bad syntax for an iteration

查看:67
本文介绍了迭代语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个循环以迭代我的变量线程: 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);
  });
}

解决方案

您好:)我认为主题的概念可能有些混乱.

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

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

知道这一点,以及这一点:

每个主题都是可观察的.

您需要订阅它才能从观察对象获得任何更改(和值).应该是这样的:

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/

I am trying to create a loop in order to iterate my variable threads : threads: Subject<{[key: string]: Thread }> = new BehaviorSubject({});

By trying this, I do not get what I want. Indeed my variable threadDictionary [key] returns me false instead of returning a Thread object to me.

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];
  }
}

Thank you for your help...

//////EDIT

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');
    }
  });
}

When I try, it does not work. I get the impression that my threadTest variable contains nothing. I add my previous threads variable to the new threadTest variable like this :

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

Here is the function I had before to subscribe to the stream :

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

解决方案

Hi :) I think there might be some confusion about the concept of Subject.

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

What is a Subject? An RxJS Subject is a special type of Observable that allows values to be multicasted to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast.

Knowing this, and this:

Every Subject is an Observable.

you need to subscribe to it in order to get any changes (and values) from the observable. It should be something like this:

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 :(');
  }
});

And your threads variable should look like this:

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

One more thing, i think you should use just Subject and not BehaviorSubject because it will emit an empty one at the beginning (unless you want to emit an initial value at the beginning).

This whole example is in a jsfiddle: https://jsfiddle.net/uLrgsr32/

这篇关于迭代语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆