2017-09-05 73 views
1

语法错误我想以我的迭代变量线程创建一个循环: 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); 
    }); 
} 
+2

[循环有关一个BehaviorSubject流(的可能的复制https://stackoverflow.com/questions/46038790/loop-for-on-a-behaviorsubject-流) –

+0

@MarkvanStraten 果然我的问题看起来是这样,但它不完全相同的问题。 – Floriane

回答

1

嗨:)我觉得可能是有关主体概念的一些混淆。

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

什么是主题? RxJS主题是一种特殊类型的Observable,它允许将值传递给许多观察者。虽然简单的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(); 

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

这整个实例是在的jsfiddle:https://jsfiddle.net/uLrgsr32/

+0

是的,我是一个初学者.. 非常感谢您的帮助,但我不知道我明白。我发表了我的文章。我们应该这样做吗?因为有错误... – Floriane

+0

我不明白为什么我不能将任何东西添加到变量线程中。当我测试onNext时,它给我一个错误。 – Floriane

+0

什么是'ths'?它是'threadDictionary'吗? – Floriane