0

I have a model named User and I have an Observer in my service called getUsers which get a bunch of JSON objects in.

In my component I have the following

@Input() users: User[] = [];

and I and trying to use it like this:

   getUserByLastName(lastName){

        this._searchService.getUsers()
            .filter(users => {
                for(let user of users) {
                    if(user.lastName == lastName){
                        this.users.push(user);
                    }}})
            .subscribe(users => this.users = users,
                error =>this.errorMessage =<any> error);

        return this.users;

    }

However this line .filter(users => { is giving me an error saying:

Argument of type '(users: User[]) => void' is not assignable to 
parameter of type '(value: User[], index: number) => boolean'. Type 
'void' is not assignable to type 'boolean

I later try to use this to initialize the variable so I can populate it on a different form.

case "lastName":
                this.users = this.getUserByLastName(this.lastName);

If that is confusing, I have two pages. One is hidden until a row is click in the default page. A form is in the other one (the hidden one) which populates data for the user (row) selected in a form to edit.

2 Answers 2

3

I assume you can have different term upon you want to filter. If not you can already pass the lastname to the service to filter, but if you want to filter in the component do the following. If you need to return the filtered data, as it seems, return it, and where you need it, subscribe. So:

getUserByLastName(lastName){
   return this._searchService.getUsers()
      .map(data => {
         return data.filter((x: any) => x.lastName == lastName);
      })
}

and where you need it, subscribe:

this.getUserByLastName(this.lastName)
  .subscribe(data => {
     this.users = data;
  });
Sign up to request clarification or add additional context in comments.

Comments

1

This can be easily achieved using lodash as below

users =<User[]> _.pullAllWith(users,{lastName : 'yourparameter'}, _.isEqual);

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.