[英]How to return an array from rxjs observable?
I've started with simply pulling in an array and mapping over the values, setting to state and then displaying on screen using conventional react and javascript methods我从简单地拉入一个数组并映射值开始,设置为 state,然后使用传统的反应和 javascript 方法在屏幕上显示
Now I want to rebuild this with rxjs现在我想用 rxjs 重建它
{
"countriesList": [
{
"name": "Australia"
},
{
"name": "Austria"
},
{
"name": "Beligum"
},
{
"name": "Belize"
},
{
"name": "Brazil"
},
{
"name": "Cameroon"
},
{
"name": "Denmark"
}
]
}
const countries$ = of(countriesList) // this returns the array above
but then I want to filter this list when I start typing values.但是当我开始输入值时,我想过滤这个列表。 typically I would build a function, and then filter based on the input.
通常我会构建一个 function,然后根据输入进行过滤。 but struggling to replicate this with rxjs
但努力用 rxjs 复制这个
I've tried the following and keep getting countries.map
is not a function我尝试了以下方法并不断获得
countries.map
地区。map 不是 function
return countries$.subscribe((countries) => countries)
I also tried this:我也试过这个:
countries$.pipe(map((country) => country.filter((ctry) => ctry.name.toLowerCase().startsWith(e.target.value))))
whereby I want to map then filter and return the values that start with the letters typed in我想 map 然后过滤并返回以键入的字母开头的值
not really getting the hang of this没有真正掌握这个
any ideas?有任何想法吗?
Ok for starters I am trying to learn rxjs
lately so I hope my example will help you understand the differences between of
and from
.好的,对于初学者来说,我最近正在尝试学习
rxjs
所以我希望我的例子能帮助你理解of
和from
之间的区别。
Also it would be nice to review the of and from "creators".回顾一下“创作者”的作品也很不错。
const { from, of, pipe } = rxjs;
const { map, filter } = rxjs.operators;
console.clear();
const countries = [
{
"name": "Australia"
},
{
"name": "Austria"
},
{
"name": "Beligum"
},
{
"name": "Belize"
},
{
"name": "Brazil"
},
{
"name": "Cameroon"
},
{
"name": "Denmark"
}
];
const countriesFrom$ = from(countries);
const countriesOf$ = of(countries);
console.log("----stream from----")
countriesFrom$.subscribe(val => console.log(val));
console.log("----stream of----")
countriesOf$.subscribe(val => console.log(val));
const countries$ = from(countries);
console.log('----filtering----');
countries$.pipe(
filter(country => country && country.name && country.name.startsWith('A')),
map(console.log)
).subscribe()
basically of
unlike from
, it does not do any flattening and emits each argument in whole as a separate next notification基本上
of
from
不同,它不做任何展平,并将每个参数作为单独的下一个通知整体发出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.