4

I have an array of Users in where every user has a name (user.name):

let users = Array<User>();

Imagine I have this array of strings I want to filter users array based on the next array of strings:

let filterargs = ['Lau', 'Za'];

This is my markup:

<li *ngFor="let user of usersList | filter: filterargs; let i = index;">

This is my pipe filter but it isn't working:

import {Pipe, Injectable, PipeTransform} from "@angular/core";

@Pipe({name: 'filter', pure: false})
@Injectable()
export class FilterPipe implements PipeTransform {

    transform(items: any[], args: any[]): any {
        // filter items array, items which match and return true will be kept, false will be filtered out
        return items.filter(item => item.name.toLowerCase().indexOf(args[0].name.toLowerCase()) !== -1);
    }
}
1
  • 1
    You should use args[0].toLowerCase() instead of args[0].name.toLowerCase() Commented Aug 27, 2016 at 2:57

2 Answers 2

6

I think your filter function is not correct. try doing :

return items.filter(item => item.name.toLowerCase().indexOf(args[0].toLowerCase()) !== -1);

Hope this Helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe this will help you:

return items.filter(item => item[0].name.toLowerCase().indexOf(args[0].toLowerCase()) !== -1);

I had a similar issue with objects in an array, by adding the [0], it worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.