Rxjs 以可观察的方式过滤阵列
下面是一个来自learnrxjs的编辑示例。我想过滤类型数组中的值。但它的工作原理并非如此:“由于类型“string[]”和“string”没有重叠,因此该条件将始终返回“true” 我是rxjs新手,不知道如何过滤数组。有什么建议吗?可能吗Rxjs 以可观察的方式过滤阵列,rxjs,Rxjs,下面是一个来自learnrxjs的编辑示例。我想过滤类型数组中的值。但它的工作原理并非如此:“由于类型“string[]”和“string”没有重叠,因此该条件将始终返回“true” 我是rxjs新手,不知道如何过滤数组。有什么建议吗?可能吗 const source = from([ { name: 'Joe', age: 31, type: ['a', 'b'] }, { name: 'Bob', age: 25, type: ['a'] }
const source = from([
{ name: 'Joe', age: 31, type: ['a', 'b'] },
{ name: 'Bob', age: 25, type: ['a'] }
]);
//filter out people with type b
const example = source.pipe(filter(person => person.type != 'a'));
//output: "People with type b: Bob"
const subscribe = example.subscribe(val => console.log(`Type a: ${val.name}`));
您正在应用的filter()
接受一个带有签名T=>boolean
的函数,这意味着您必须返回布尔值true/false,以便它可以从流中过滤出元素
您的元素T
属于对象{name:string,age:number,type:array}
类型,因此要过滤类型
数组中的值,您需要使用:
您正在应用的filter()
接受一个带有签名T=>boolean
的函数,这意味着您必须返回布尔值true/false,以便它可以从流中过滤出元素
您的元素T
属于对象{name:string,age:number,type:array}
类型,因此要过滤类型
数组中的值,您需要使用:
source.pipe(filter(person => person.type.indexOf('b') == -1) // filter out people who have type b