2017-09-04 93 views
0

我想重複我的變量線程:迭代在流

threads: Subject<{[key: string]: Thread }> = new BehaviorSubject({}); 

儘管我多次嘗試:

return this.threads.asObservable().map((threadDictionary: {[key: string]: Thread}) => { 
    for(let key in threadDictionary) { 
    console.log("key", threadDictionary[key]); 
    if(threadDictionary[key].participants[0].name.startsWith(str)) { 
     return threadDictionary[key]; 
    } 
    } 
}); 


return this.threads.map((threadDictionary: {[key: string]: Thread}) => { 
.flatMap((threadDictionary: {[key: string]: Thread}) => { 
    for(let key in threadDictionary) { 
     console.log("key", threadDictionary[key]); 
     if(threadDictionary[key].participants[0].name.startsWith(str)) { 
     return threadDictionary[key]; 
     } 
    } 
    }); 
}); 

////////////// /////// 編輯 /////////////////////

return this.threads.asObservable().map((threadDictionary: {[key: string]: Thread}) => { 
    return threadDictionary[key]; 
}); 

    for(let key in threadDictionary) { 
    console.log("key", threadDictionary[key]); 
    if(threadDictionary[key].participants[0].name.startsWith(str)) { 
     return threadDictionary[key]; 
    } 
    } 

我想知道這是可能創造一個For循環在我的可變線程上。我應該改變它的變量類型嗎?或者如何使這個循環?

回答

0

您不能迭代threadDictionary參數,因爲它不是數組。您可以嘗試返回調用.map()的值,然後迭代結果。

var threadDictionary = this.threads.asObservable().map((threadDictionary: {[key: string]: Thread}) => { 
    return threadDictionary; 
}); 

for(let key in threadDictionary) { 
    console.log("key", threadDictionary[key]); 
    if(threadDictionary[key].participants[0].name.startsWith(str)) { 
    return threadDictionary[key]; 
    } 
} 
+0

對不起,我不明白你的解決方案。 我發表了我的文章,但我不認爲它是這樣的.. – Floriane

+0

你有一個例子嗎? – Floriane

+0

查看我剛剛提供的示例。 –