简体   繁体   中英

Filtering array with value from Observable/subscribe?

I use ngx-translate to translate keyword through my app, including a list of tags. User should be able to search through them in any language.

Previously I was doing

this.tag_array_filtered = this.tag_array.filter(tag => 
   tag.toUpperCase().includes(this.tag_searched.toUpperCase()));
)

but it only search the key word, not through translations.

this is how I get a translation from a key

this.translate.get(tag).subscribe(value => {
   console.log(value);
})

the variable tag_array is an array of key, ready to be translated. tag_searched contain the user input. tag_array_filtered is the array of key (not translated) to be displayed

What I want to do: (not working obviously)

 this.tag_array_filtered = this.tag_array.filter(tag => 
    this.translate.get(tag.toUpperCase()).subscribe((value: string) => {
      value.includes(this.tag_searched.toUpperCase())
  })
 )

How can I filter my array while transforming the value with a subscribe ? I want to compare each value translated from the array to the user input.

I could do it with hand-made loops I guess but if there is a solution already existing it would probably be much faster.

The translate.get() method also accepts an array of strings. You could try like this. The final output will be an object which you could transform it into an array if needed.

 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 }) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM