在 Observable 中使用正则表达式进行过滤
Filter with regex inside Observable
我想过滤不区分大小写。
我尝试了很多方法但没有成功。
它适用于包含但如果我在过滤器中应用新的正则表达式或者我使用匹配
则没有成功
filterProperties() {
this.propertiesService.getProperties().pipe(
map(
// (properties) => properties.filter(property => property.title.includes('LON')))
(properties) => properties.filter(
property => {
const title = new RegExp('london', 'i');
property.title.match(title);
}
)),
tap(x => x)
)
.subscribe(
properties => this.properties = properties
);
console.log(this.properties);
}
filter
的回调应该 return 一些东西,includes
它有效,因为你 return true/false
includes
的值,箭头如果 {}
丢失,return 的单行自动运行,这就是你的情况 property => property.title.includes('LON')
,regexp
它不起作用,因为你没有 return一些东西,要修复它添加 return
properties => properties.filter(
property => {
const title = new RegExp('london', 'i');
return property.title.match(title);
}
))
我想过滤不区分大小写。 我尝试了很多方法但没有成功。 它适用于包含但如果我在过滤器中应用新的正则表达式或者我使用匹配
则没有成功 filterProperties() {
this.propertiesService.getProperties().pipe(
map(
// (properties) => properties.filter(property => property.title.includes('LON')))
(properties) => properties.filter(
property => {
const title = new RegExp('london', 'i');
property.title.match(title);
}
)),
tap(x => x)
)
.subscribe(
properties => this.properties = properties
);
console.log(this.properties);
}
filter
的回调应该 return 一些东西,includes
它有效,因为你 return true/false
includes
的值,箭头如果 {}
丢失,return 的单行自动运行,这就是你的情况 property => property.title.includes('LON')
,regexp
它不起作用,因为你没有 return一些东西,要修复它添加 return
properties => properties.filter(
property => {
const title = new RegExp('london', 'i');
return property.title.match(title);
}
))