寶慕林4294392
2023-04-27 16:08:39
這是應(yīng)用程序,我想創(chuàng)建具有不同類別的不同屏幕,在這種情況下,我有 Dermatologista 和 Hospital,如何只選擇一個描述 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(我只想選擇描述為 == 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> ) }
2 回答

RISEBY
TA貢獻1856條經(jīng)驗 獲得超5個贊
您可以使用array.filter
:
const filteredPlaces = state.places.filter( place => place.description === "Dermatologista" )
并將filteredPlaces
整個對象而不是整個對象傳遞給子組件。

動漫人物
TA貢獻1815條經(jīng)驗 獲得超10個贊
嘗試這個
<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>
添加回答
舉報
0/150
提交
取消