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