Rxjs:Rxjs中是否有可以在数组未映射时执行的运算符?
我有一个静态数组Rxjs:Rxjs中是否有可以在数组未映射时执行的运算符?,rxjs,observable,Rxjs,Observable,我有一个静态数组 var array=[{name:'test',id:2106},{name:'test2',id:2107},{name:'test3',id:2108}]; I have a selected name var selName='test4'; 我想在selName与数组对象中的任何名称不匹配时执行函数 设函数为 function runtesting(){ } 如果selName与数组中的名称匹配,我希望显示一个警报框 我们如何使用Rxjs操作符实现这一点 con
var array=[{name:'test',id:2106},{name:'test2',id:2107},{name:'test3',id:2108}];
I have a selected name
var selName='test4';
我想在selName与数组对象中的任何名称不匹配时执行函数
设函数为
function runtesting(){
}
如果selName与数组中的名称匹配,我希望显示一个警报框
我们如何使用Rxjs操作符实现这一点
const array$ = Observable.of([
{name:'test', id:2106},
{name:'test2', id:2107},
{name:'test3', id:2108}
])
const selectedName$ = new BehaviorSubject('test4')
然后将两者结合起来:
const runTestingSub = Observable
.combineLatest(array$, selectedName$)
.map(([arr, name]) => !arr.some(item => item.name === name))
.filter(Boolean) // Emit only if there is no name in array
.subscribe(runTesting) // runTesting runs only if there is no obj with name
通过将此数组存储为键值结构,您甚至可以提高其性能,如:
{
test: { name: 'test', id: 2106 },
test2: { name: 'test2', id: 2107 }
}
甚至JavaScript/ImmutableMap
。例如:
const arrayReduced$ = Observable
.of([
{name:'test', id:2106},
{name:'test2', id:2107},
{name:'test3', id:2108}
])
.scan((acc, curr) => Object.assign({}, acc, {
[curr.name]: curr
}), {})
然后runTestingSub
将如下所示:
const runTestingSub = Observable
.combineLatest(array$, selectedName$)
.map(([keyValueObj, name]) => !keyValueObj[name])
.filter(Boolean)
.subscribe(runTesting)
这看起来不是一个使用rxjs的好例子。。。。看不到任何溪流。如果selName是一个流,那么
selNameStream.subscribe(s=>if([s在数组中]){showartbox();}否则{runTesting();}
它不是一个流,我想使用rxjs,因为下面的函数是异步触发的