1

im trying to filter my api array in this just for the users witch speciality == Dermatologista, can you help me?

  export default class Dermatologistas extends Component{
      state ={
        errorMessage: null,
        users: []
      }

here I create the async function that is getting the users

getUserList = async () => {
    try {
      const response = await api.get('/auth/list');

      const { users } = response.data
      console.log(response.data)
      this.setState({ users });
     
    } catch (response) {
      this.setState({ errorMessage: response.data.error });
    }
  };
  
  componentDidMount() {
    this.getUserList()
  }

here it renders all users and not filter, how can I do this ?

render(){
        const users = this.state.users
        console.log(users)
        return(
          <View >
            { this.state.errorMessage && <Text>{ this.state.errorMessage }</Text> }
            {this.state.users.map(user => (
              <View key={user._id} style={{marginTop: 15, alignItems: 'center'}}>
                <Text>{user.title}</Text>
                <Text>{user.speciality}</Text>
                <Button   title = 'View Profile'onPress ={() => 
                   this.props.navigation.navigate('Profile')}/>
                </View>
            ))}
               </View>
        )
      }
    }

3 Answers 3

1

You can create a function which takes the data that's being received along with the parameter through which you want to filter the data.

const filterBySpeciality = (data, speciality) => data.filter(d => d.speciality == speciality)

let data = [{speciality: "Ortho", title: "Chief Surgeon"}, {speciality: "Dermatologista", title: "Specialist"}];

console.log(filterBySpeciality(data, "Dermatologista"));

So, in your case you can call filterBySpeciality(data, "Dermatologista") in your render function and use the result for populating the View like below.

const filterBySpeciality = (data, speciality) => data.filter(d => d.speciality == speciality)

render() {
   const filteredData = this.filterBySpeciality(this.state.users, "Dermatologista");
   return(
          <View >
            { this.state.errorMessage && <Text>{ this.state.errorMessage }</Text> }
           {/* Here I have changed this.state.users to filteredData */}
            {filteredData.map(user => (
              <View key={user._id} style={{marginTop: 15, alignItems: 'center'}}>
                <Text>{user.title}</Text>
                <Text>{user.speciality}</Text>
                <Button   title = 'View Profile'onPress ={() => 
                   this.props.navigation.navigate('Profile')}/>
                </View>
            ))}
               </View>
        )

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

Comments

0

Just put condition before rendering to data.

getUserList = async () => {
    try {
        const response = await api.get('/auth/list');

        const usersData = response.data
        const users = usersData.filter((user: any) => {
             if (user.speciality === 'Dermatologista') return user;
        })
        console.log(users);
        this.setState({ users });

    } catch (response) {
        this.setState({ errorMessage: response.data.error });
    }
};

componentDidMount() {
    this.getUserList()
}

Or you can try something like this :

render(){
    const usersData = this.state.users
    const users = usersData.filter((user: any) => {
         if (user.speciality === 'Dermatologista') return user;
    })
    console.log(users)
    return(
      <View >
        { this.state.errorMessage && <Text>{ this.state.errorMessage }</Text> }
        {this.state.users.map(user => (
          <View key={user._id} style={{marginTop: 15, alignItems: 'center'}}>
            <Text>{user.title}</Text>
            <Text>{user.speciality}</Text>
            <Button   title = 'View Profile'onPress ={() => 
               this.props.navigation.navigate('Profile')}/>
            </View>
        ))}
           </View>
    )
  }
}

Comments

0

in the view just this:

     <View >
        { this.state.errorMessage && <Text>{ this.state.errorMessage }</Text> }
        {this.state.users.filter(user => user.speciality === 'Dermatologista').map(user => (
          <View key={user._id} style={{marginTop: 15, alignItems: 'center'}}>
            <Text style={{fontWeight: 'bold', fontSize:20}}>{user.title}</Text>
            <Text>{user.speciality}</Text>
            <Button   title = 'View Profile'onPress ={() => this.props.navigation.navigate('Profile')}/>
       </View>

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.