I m retrieving some JSON data which has more than 2 attributes for the same item as follows into Picker in React Native Flatlist:
{"products":
[
{
"name": "Dried Blueberry",
"weight": "1000gm",
"price": "200"
},
{
"name": "Dried Blueberry",
"weight": "500gm",
"price": "100"
},
{
"name": "Dried Blueberry",
"weight": "250gm",
"price": "50"
}
]
}
It is displaying as shown in this
But I want to display it as this
Anyone will help me?
Here's my code:
renderProductsCart = ({ item }) => {
return (
<View style={{ width: "46%", marginLeft: 10, marginTop: 10 }}>
<Card
elevation={2}>
<Card.Cover source={{ uri: item.image }} />
<Text style={{ marginLeft: 5 }}>{item.name}</Text>
<Text> ₹: {item.attribute_price}/- </Text>
<Picker
selectedValue={this.state.data}
onValueChange={(itemValue, itemIndex) => this.setState({ data: itemValue })} >
{/* {this.state.city.map((item, key) => (
<Picker.Item label={item.name} value={item.name} key={key} />)
)} */}
<Picker.Item label={item.attribute_name} value={item.attribute_name} />
</Picker>
<View style={{ flexDirection: "row" }}>
<Card.Actions>
<Button
theme={{ colors: { primary: 'black' } }}
onPress={() => console.log("press")}>View More</Button>
<Button
theme={{ colors: { primary: 'black' } }}
onPress={() => console.log("press")}>Cart</Button>
</Card.Actions>
</View>
</Card>
</View>
)
}
<FlatList
data={this.state.data.attributes}
renderItem={this.renderProductsCart}
keyExtractor={(item, index) => index.toString()}
numColumns={2}
/>
as mentionned by Aymen Guidad, you must massage your data to an object containing an array of values.
Using reduce, it can be achieved like so:
Now, on React, you must pass that to FlatList, except it expects an array, so you must do something like outline in this question: React Native FlatList with object
You can
map
normally over your array in your Picker.