如何过滤结果?
我调用端点,并以下一种形式接收数据:
{"children":["Folder1/Data1","Folder1/Data2","Folder1/Data3","Folder1/Data4","Folder1/Value_1","Folder1/Value_2"],"path":"Folder1/Data","type":"Folder"}
我想通过只获取包含单词Value的子项来过滤结果,例如:"Folder1/Value_1" and "Folder1/Value_2"
到目前为止,我有下一个:
我从我的组件订阅此服务
public recorders: Model[];
getValues() {
this._service.getValues().subscribe(
values => { console.log(values) },
error => this.errorMessage = <any>error
);
}
服务:
getValues(): Observable<Model[]> {
let url = "some url";
let subString = 'Value';
return this.http.get(url)
.map(this.extractData)
.filter((body) => body.includes(subString))
.catch(this.handleError);
}
private extractData(res: Response, item) {
let body = res.json().children;
return body;
}
private handleError(error: any) {
let errMsg = error.message || 'Server error';
console.error(errMsg);
return Observable.throw(errMsg);
}
我也尝试过:
.filter((body) => { return (body.indexOf("Value") === -1) }) or
.map(data => data.filter(data => data.indexOf('Value') === -1))
但它不起作用,我的意思是订阅中的console.log根本不打印任何东西,但是如果我在“map”中写一个console.log,我可以打印所有结果(extractData法)。
最后,我想得到包含字符串“Value”的“children”,因为我想对每一个进行一次新的http调用(通过将它们与url组合) 例如“some url”/ Value_1,但这是另一个故事。
谢谢!