簡體   English   中英

使用Observable / subscribe的值過濾數組?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM