我在返回语句的函数中循环遍历嵌套数组时遇到问题。
selectInputFilter(enteredText, filter) {
if (this.searchType === 3) {
return (enteredText['actors'][0]['surname'].toLocaleLowerCase().indexOf(filter) !== -1);
}
}
所以基本上不是手动指向[actor]的[0]元素,而是想在点处遍历数组的所有元素。但是我不知道如何在这样的返回函数中嵌入一个循环。我试过forEach但我的VSCode正在抛出错误。
我需要嵌套的完整管道在下面。所以基本上我不能在最后的else语句中包含循环。最重要的是,这些代码在这一点上看起来很像意大利面,因此欢迎任何简化建议。
export class InputFilterPipe implements PipeTransform{
searchType: number;
private subscription: Subscription;
constructor(private movieService: MovieService) {
this.movieService.getSearchType().subscribe(
id => this.searchType = id
);
}
transform(value: any[], filter: string): any[] {
filter = filter ? filter.toLocaleLowerCase() : null;
return filter ? value.filter(
(arraySearched) =>
this.selectInputFilter(arraySearched, filter))
: value;
}
selectInputFilter(arraySearched, filter) {
if (this.searchType === 3) {
const values = [];
for (let actor of arraySearched['actors']) {
values.push(actor['surname'].toLocaleLowerCase().indexOf(filter) !== -1);
for (let i = 0; i < values.length; i++ ) {
if (values[i] === true) {
return (arraySearched['actors'][i]['surname'].toLocaleLowerCase().indexOf(filter) !== -1);
}
}
}
} else if (this.searchType === 2) {
return (arraySearched['director'].toLocaleLowerCase().indexOf(filter) !== -1);
} else if (this.searchType === 1) {
return (arraySearched['title'].toLocaleLowerCase().indexOf(filter) !== -1);
} else {
return (arraySearched['title'].toLocaleLowerCase().indexOf(filter) !== -1) ||
(arraySearched['director'].toLocaleLowerCase().indexOf(filter) !== -1) ||
(arraySearched['actors'][0]['surname'].toLocaleLowerCase().indexOf(filter) !== -1) ||
(arraySearched['actors'][1]['surname'].toLocaleLowerCase().indexOf(filter) !== -1) ||
(arraySearched['actors'][2]['surname'].toLocaleLowerCase().indexOf(filter) !== -1);
// (arraySearched['actors'][3]['surname'].toLocaleLowerCase().indexOf(filter) !== -1);
}
}
答案 0 :(得分:0)
你可以创建一个生成器函数,如下所示:
function* selectInputFilter(enteredText, filter) {
if (this.searchType === 3) {
for (var actor of enteredText['actors']) {
yield actor['surname'].toLocaleLowerCase().indexOf(filter) !== -1;
}
}
}
使用类似这样的东西:
for (var x of selectInputFilter(/*your args here*/)) {
// x will loop over each of the "returned" values
}
或者,您可以创建并返回所需值的数组,如下所示:
function selectInputFilter(enteredText, filter) {
if (this.searchType === 3) {
var values = [];
for (var actor of enteredText['actors']) {
values.push(actor['surname'].toLocaleLowerCase().indexOf(filter) !== -1);
}
return values;
}
}
修改强>
对于后一种情况,您可以使用Array.prototype.some
,如此
} else {
return (arraySearched['title'].toLocaleLowerCase().indexOf(filter) !== -1) ||
(arraySearched['director'].toLocaleLowerCase().indexOf(filter) !== -1) ||
(arraySearched['actors'].some((actor) => actor['surname'].toLocaleLowerCase().indexOf(filter) !== -1));
}
或者只使用这样的变量:
} else {
let returnValue = (arraySearched['title'].toLocaleLowerCase().indexOf(filter) !== -1) ||
(arraySearched['director'].toLocaleLowerCase().indexOf(filter) !== -1);
for (let actor of arraySearched['actors']) {
if (returnValue) break; // so it doesn't keep looping after returnValue is true
returnValue = returnValue || actor['surname'].toLocaleLowerCase().indexOf(filter) !== -1);
}
return returnValue;
}