Having trouble looping through nested arrays in a function that returns a statement.
selectInputFilter(enteredText, filter) {
if (this.searchType === 3) {
return (enteredText['actors'][0]['surname'].toLocaleLowerCase().indexOf(filter) !== -1);
}
}
Instead of manually specifying the [0] element of [actors], I would like to iterate through all elements of the array. However, I'm not sure how to implement a loop inside such a returning function. I attempted using forEach but encountered errors in my VSCode.
The complete pipe where nesting is needed is provided below. Unfortunately, it's not possible to include a loop within the last else statement. Additionally, the code seems a bit messy at this point, so any suggestions on simplifying it are appreciated.
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);
}
}