[英]Filtering array with value from Observable/subscribe?
我使用ngx-translate通過我的應用程序翻譯關鍵字,包括標簽列表。 用戶應該能夠以任何語言搜索它們。
以前我在做
this.tag_array_filtered = this.tag_array.filter(tag =>
tag.toUpperCase().includes(this.tag_searched.toUpperCase()));
)
但它僅搜索關鍵字,而不通過翻譯。
這就是我從鑰匙那里得到翻譯的方法
this.translate.get(tag).subscribe(value => {
console.log(value);
})
變量tag_array
是一個鍵數組,可以進行翻譯。 tag_searched
包含用戶輸入。 tag_array_filtered
是要顯示的鍵的數組(未翻譯)
我想做什么:(顯然無法正常工作)
this.tag_array_filtered = this.tag_array.filter(tag =>
this.translate.get(tag.toUpperCase()).subscribe((value: string) => {
value.includes(this.tag_searched.toUpperCase())
})
)
如何在通過訂閱轉換值時過濾數組? 我想比較從數組轉換到用戶輸入的每個值。
我猜我可以用手工制作的循環來做,但是如果已經有解決方案,那可能會更快。
translate.get()
方法還接受字符串數組。 您可以嘗試這樣。 最終輸出將是一個對象,您可以根據需要將其轉換為數組。
let tag_array_filtered = this.tag_array.filter(tag => tag.toUpperCase() .includes(this.tag_searched.toUpperCase()) .map(e => e.toUpperCase()) this.translate.get(matching_tags).subscribe((values: {}) => { this.tag_array_filtered_object = values })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.