gpt4 book ai didi

react-native - 如何使用数据中对象的嵌套循环呈现 SectionList

转载 作者:行者123 更新时间:2023-12-05 03:59:56 32 4
gpt4 key购买 nike

我有下面的json结构

{
"title": "Name(s)",
"type": "Text",
"data": [
{
"source": "DB",
"title": "All",
"list": [
{
"name": "ABCD",
"count": 1
},
{
"name": "BCDE",
"count": 1
},
{
"name": "CDEF",
"count": 1
},
{
"name": "DEFG",
"count": 2
},
{
"name": "EFGH",
"count": 1
}
]
}
]
},
{
"title": "Category(s)",
"type": "Text",
"data": [
{
"source": "DB",
"title": "All",
"list": [
{
"name": "Vegetables",
"count": 1942
},
{
"name": "Saloon",
"count": 355
},
{
"name": "General Store",
"count": 331
},
{
"name": "Restaurants",
"count": 130
},
{
"name": "Fast Food",
"count": 108
}
]
}
]
}

我正在尝试将数据显示为“喜欢”

第一个 SectionHeader:“姓名”
第一行:“ABCD”
第二行“BCDE”
第三行“CDEF”
.
.
.

第二节标题:“类别”第一行:“蔬菜”
第二排“沙龙”
第三行“杂货店”
.
.
.
在这里,我应该使用 SectionList/Flatlist/Mix 两者来得到上面的结果。

在 flatlist/sectionlist 中,我在 renderSectionHeader 中获取节标题 Names(s)& Category(s),但在 renderItem 中,如何循环对象的“列表”数组。请告诉我

最佳答案

您必须像下面这样更新数据,

例如:

[
{
"title": "Name(s)",
"type": "Text",
"data": [
{
"name": "ABCD",
"count": 1
},
{
"name": "BCDE",
"count": 1
},
...
]
},
{
"title": "Category(s)",
"type": "Text",
"data": [
{
"name": "Vegetables",
"count": 1942
},
{
"name": "Saloon",
"count": 355
},
...
]
}
...
]

并使用SectionList来显示它,

例如:

 ...
<SectionList
renderItem={({item, index, section}) => <Text key={index}>{item.name}</Text>}
renderSectionHeader={({section: {title}}) => (
<Text style={{fontWeight: 'bold'}}>{title}</Text>
)}
sections={this.state.data}
keyExtractor={(item, index) => item + index}
/>
...

关于react-native - 如何使用数据中对象的嵌套循环呈现 SectionList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56786687/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com