下面是我的JSON,
"[{"id":32156465,"b_name":"KFC","website":"kfc.com","category":[]},{"id":87985645,"b_name":"Pizza Hut","website":"pizzahut.com","category":[{"id":"357","title":"Pizza"}]},{"id":78956465,"b_name":"Mc Donalds","website":"mcdonalds.com","category":[{"id":"951","title":"Burger"}]},{"id":32136556,"b_name":"Eagle Boys Pizza","website":null,"category":[]}]"
需要迭代类别数组,如果存在任何对象,则需要获取标题并显示为:
Category: Testing
否则,不应显示“类别:”文本。
这就是我的全部代码
export default class App extends Component<Props> {
constructor(props) {
super(props)
this.state = { showCategoryList: false }
}
categoriesList(categoriesObject) {
var categoriesListStr = ""
categoriesObject.map((categoryDict) => {
Object.keys(categoryDict).map(function (key, value) {
console.log("Key- " + key + "Value-" + value)
if ((key == "title") && (categoryDict[key] !== null)) {
categoriesListStr = categoryDict[key]
}
})
})
return categoriesListStr
}
_renderItem = ({ item, section }) => {
return (
<View style={styles.rowViewContainer1}>
<Text style={{fontSize: 12, fontWeight: "normal", marginLeft: 10}}>{'Business Name: ' + item.b_name}</Text>
<Text style={{fontSize: 12, fontWeight: "normal", marginLeft: 10}}>{'Website: ' + item.website}</Text>
<Text style={{fontSize: 12, fontWeight: "normal", marginLeft: 10}}>{'Category: ' + this.categoriesList(item.category)}</Text>
{/* here, if categoriesListStr returns empty string, then we should not display"Category: ", how can i achieve this */}
<View style={styles.lineViewSeparation} />
</View>
)
};
_renderSectionHeader = ({ section }) => {
return (
<View style={styles.sectionHeader}>
<Text style={styles.header}>{section.key}</Text>
</View>
)
}
render() {
var prospectsDataArr = [{"id":32156465,"b_name":"KFC","website":"kfc.com","category":[]},{"id":87985645,"b_name":"Pizza Hut","website":"pizzahut.com","category":[{"id":"357","title":"Pizza"}]},{"id":78956465,"b_name":"Mc Donalds","website":"mcdonalds.com","category":[{"id":"951","title":"Burger"}]},{"id":32136556,"b_name":"Eagle Boys Pizza","website":null,"category":[]}]
var prospectsDict = { key: 'New', data: prospectsDataArr }
dataSourceArr = [prospectsDict]
console.log("renderedItem " + this._renderItem)
return (
<View style={{ marginTop: (Platform.OS) == 'ios' ? 20 : 0 }}>
<SectionList style={styles.cellView}
sections={dataSourceArr}
renderItem={this._renderItem}
renderSectionHeader={this._renderSectionHeader}
keyExtractor={(item, index) => index}
/>
</View>
);
}
}
在这里,如果categoriesListStr返回空字符串,那么我们不应该显示“Category:”,我如何实现这一点