2017-09-05 70 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