mewi
mewi

Reputation: 352

Filtering in Angular 2

I'm currently following an oldish Angular tutorial on Pluralsight and was told to enter the following code

    performFilter(filterBy: string): IProduct[] {
    filterBy = filterBy.toLocaleLowerCase;
    return this.products.filter((product: IProduct) => product.productName.toLocaleLowerCase.indexOf(filterBy) !== -1);
}

Which is supposed to filter a user search. (ie they type the letter 'a', only results containing that letter will display. However, I'm getting a few errors:

1: [ts] Type '() => string' is not assignable to type 'string'. (parameter) filterBy: string

2: [ts] Property 'indexOf' does not exist on type '() => string'. any

I'm new to typescript and not sure if I'm supposed to be casting these variables or something entirely different.

Upvotes: 0

Views: 125

Answers (2)

Krishna Rathore
Krishna Rathore

Reputation: 9687

You can try this solution use toLowerCase()

performFilter(filterBy: string): IProduct[] {
    filterBy = filterBy.toLowerCase();
    return this.products.filter((product: IProduct) => product.productName.toLowerCase().indexOf(filterBy) !== -1);
}

Upvotes: 1

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249636

toLocaleLowerCase is a function, you should call it, the idea is to get the lower case version of the original string:

interface IProduct { productName: string}
function performFilter(filterBy: string): IProduct[] {
    filterBy = filterBy.toLocaleLowerCase();
    return this.products.filter((product: IProduct) => product.productName.toLocaleLowerCase().indexOf(filterBy) !== -1);
}

Upvotes: 1

Related Questions