React native 如何根据数组的键向数组中添加不同的项?
我有这个从后端获取的数组! 这是数组React native 如何根据数组的键向数组中添加不同的项?,react-native,React Native,我有这个从后端获取的数组! 这是数组 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]
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}>
{CATEGORIES_TYPES[item.name] && (
<FontAwesome name={CATEGORIES_TYPES[item.name]} style={CATEGORIES_TYPES[item.name].style} />
)}
<Text>{item.name}</Text>
</View> ))
{this.state.categories.results.map((项,键)=>(
{CATEGORIES_TYPES[item.name]&&(
)}
{item.name}
))
但这不起作用。这是一种基于键添加的方法吗?我认为这就是您想要做的
const styles=StyleSheet.create({
汽车:{
宽度:50,
尺寸:40,
身高:60,
颜色:“#f56217”
},
主页:{
宽度:50,
尺寸:40,
身高:60,
颜色:“#2c3e50”
},
电视:{
宽度:50,
尺寸:40,
身高:60,
颜色:“黑色”
}
});
{this.state.categories.results.map((项,键)=>(
{item.name}
))
使用条件渲染,或使用图标名称和类别名称作为键的对象
const Icons = {
Home: 'home',
Electronics: 'microchip',
Vehicles: 'car',
};
然后在渲染中执行以下操作:
{this.state.categories.results.map((item, key) => (
<View key={key}>
{CATEGORIES_TYPES[item.name] && (
<FontAwesome
name={Icons[item.name]}
style={CATEGORIES_TYPES[item.name].style}
/>
)}
<Text>{item.name}</Text>
</View>
))}
你能添加
这个.state.categories.results中的内容吗?
?顺便说一句,这里使用的键
只是数组中每个元素的索引,如0
。1
。2
。我添加了数组,但我也希望图标的名称与style@amanirose就像我的回答,三个不同的ic是的,你喜欢你的答案,但是我们可以用钥匙吗,因为它是唯一的。你的钥匙是什么?你的意思是日期?好主意,谢谢。谢谢。谢谢。谢谢。如果我按了这些类别中的任何一个,我怎么能做到呢导航到不同的页面??按添加并导航到您的deisred屏幕。我知道,但每个类别都有一个特殊的屏幕!!!
const Icons = {
Home: 'home',
Electronics: 'microchip',
Vehicles: 'car',
};
{this.state.categories.results.map((item, key) => (
<View key={key}>
{CATEGORIES_TYPES[item.name] && (
<FontAwesome
name={Icons[item.name]}
style={CATEGORIES_TYPES[item.name].style}
/>
)}
<Text>{item.name}</Text>
</View>
))}
const CATEGORIES_TYPES = {
Vehicles: {
style: { width: 50, fontSize: 40, height: 60, color: '#f56217' },
},
Home: {
style: { width: 50, fontSize: 40, height: 60, color: '#2c3e50' },
},
Electronics: {
style: { width: 50, fontSize: 40, height: 60, color: 'black' },
},
};