語法錯誤我想以我的迭代變量線程創建一個循環: 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);
});
}
[循環有關一個BehaviorSubject流(的可能的複製https://stackoverflow.com/questions/46038790/loop-for-on-a-behaviorsubject-流) –
@MarkvanStraten 果然我的問題看起來是這樣,但它不完全相同的問題。 – Floriane