I have this array that fetched from backend ! this is the array.
resultes
[
[front] [18:52:18] Object {
[front] [18:52:18] "cate_id": 1,
[front] [18:52:18] "name": "Vehicles",
[front] [18:52:18] },
[front] [18:52:18] Object {
[front] [18:52:18] "cate_id": 2,
[front] [18:52:18] "name": "Home",
[front] [18:52:18] },
[front] [18:52:18] Object {
[front] [18:52:18] "cate_id": 3,
[front] [18:52:18] "name": "Electronics",
},]
{this.state.categories.results.map((item ,key)=>(
<View key={key}>
<Text>{item.name}</Text>
</View> ))
How can add for every item.name a different icon based on the key of the array? i tried to make an object
const CATEGORIES_TYPES = {
car: {
style: { width: 50, fontSize: 40, height: 60, color: '#f56217' }
},
home: {
style: { width: 50, fontSize: 40, height: 60, color: '#2c3e50' }
},
tv: {
style: { width: 50, fontSize: 40, height: 60, color: 'black' }
}
};
and in render:
{this.state.categories.results.map((item ,key)=>(
<View key={key}>
{CATEGORIES_TYPES[item.name] && (
<FontAwesome name={CATEGORIES_TYPES[item.name]} style={CATEGORIES_TYPES[item.name].style} />
)}
<Text>{item.name}</Text>
</View> ))
but that don't work Is this a method to add based on the key???
this.state.categories.results? btw, thekeyused here is just the index of each element in the array, like0..1..2..