简体   繁体   English

如何使用 item.description 在列表视图中选择一个项目

[英]how do I select an Item in list view with item.description

here is the app, I want to create diferent screens with diferent catergories in this case I have Dermatologista and Hospital, how can I select just one description这是应用程序,我想创建具有不同类别的不同屏幕,在这种情况下,我有皮肤科和医院,我如何只选择一个描述

 const [state, setState] = useState({
     places: [
       {
         id: 1,
         title: 'Clinica da pele',
         description: 'Dermatologista',
         latitude:-2.42206406,
         longitude:-54.71947789,
       },
       {
         id: 2 ,
         title:'Unimed',
         description:'Hospital',
         latitude:-2.42501721,
         longitude:-54.71146077,
        },
       {
         id: 3,
         title: 'Dra. Josimar',
         description:'Dermatologista',
         latitude: -2.4288346,
         longitude:-54.7290553,
       }
     ]
   });

   return(

I just want to select the items with the description == dermatologista how can I do this ?我只想选择带有描述 == dermatologista 的项目我该怎么做?

   <SafeAreaView>
        <FlatList
     styles = {styles.PlaceContainer}
     showsVerticalScrollIndicator
     data={state.places}
     keyExtractor={item => item.id}
     renderItem={({ item }) => {
       return(
         <View key={item.id} style={styles.place} >
           <Text>{item.title}</Text>
           <Text>{item.description}</Text>
         </View>
       )

     }
 

}
/>

</SafeAreaView>

  
  
   

) } ) }

You can use array.filter :您可以使用array.filter

const filteredPlaces = state.places.filter( place => place.description === "Dermatologista" )

and pass filteredPlaces instead of the entire object to the child component.并将filteredPlaces而不是整个对象传递给子组件。

Try this尝试这个

<SafeAreaView>
        <FlatList
     styles = {styles.PlaceContainer}
     showsVerticalScrollIndicator
     data={state.places}
     keyExtractor={item => item.id}
     renderItem={({ item }) => {
       item.description == "dermatologista" ? (
         <View key={item.id} style={styles.place} >
           <Text>{item.title}</Text>
           <Text>{item.description}</Text>
         </View>
       ):""

     }
 

}
/>

</SafeAreaView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM