如何循环 json 数组并在不知道 Ract Native 中的键的情况下打印值?
How to loop over a json array and print the value without knowing the key in Ract Native?
目前,我正在使用 map 显示带有键名的值,但是 JSON 会动态变化,所以我不知道 JSON 数组的键。如何在不知道 JSON 的密钥的情况下访问它?
我的Json:(但密钥会有所不同)
{
"records": [{
"DATE": "15/10/2020",
"AMT": "103284",
"TAX": "8958",
"TOTAL": "112242"
},
{
"DATE": "16/10/2020",
"AMT": "2336",
"TAX": "209",
"TOTAL": "2545"
}
]
}
当前代码:
{
sortedData && sortedData.length > 0 ?
sortedData.map((item, index) => {
return (
<>
<View style={{ flexDirection: 'row' }}>
<View style={styles.container2}>
<View style={styles.item}>
<Text>{item.DATE}</Text>
</View>
<View style={styles.item2}>
<Text>{item.AMT}</Text>
</View>
<View style={styles.item2}>
<Text>{item.TAX}</Text>
</View>
<View style={styles.item2}>
<Text>{item.TOTAL}</Text>
</View>
</View>
</View>
<Divider />
</>
);
}) : false
}
我试过下面的代码
Object.keys 将为您提供对象和映射函数的键 returns 数组格式的键的所有值。
{Object.keys(sortedData).map(key => sortedData[key])}
这可能有帮助
<div>
{arr.map(obj =>
Object.entries(obj).map(([key, value]) => (
<tr key={key}>
<td>Key: {key}</td>
<td>Activity Type: {value.activityType}</td>
<td>DurationInSeconds: {value.durationInSeconds}</td>
</tr>
)))}
</div>
目前,我正在使用 map 显示带有键名的值,但是 JSON 会动态变化,所以我不知道 JSON 数组的键。如何在不知道 JSON 的密钥的情况下访问它?
我的Json:(但密钥会有所不同)
{
"records": [{
"DATE": "15/10/2020",
"AMT": "103284",
"TAX": "8958",
"TOTAL": "112242"
},
{
"DATE": "16/10/2020",
"AMT": "2336",
"TAX": "209",
"TOTAL": "2545"
}
]
}
当前代码:
{
sortedData && sortedData.length > 0 ?
sortedData.map((item, index) => {
return (
<>
<View style={{ flexDirection: 'row' }}>
<View style={styles.container2}>
<View style={styles.item}>
<Text>{item.DATE}</Text>
</View>
<View style={styles.item2}>
<Text>{item.AMT}</Text>
</View>
<View style={styles.item2}>
<Text>{item.TAX}</Text>
</View>
<View style={styles.item2}>
<Text>{item.TOTAL}</Text>
</View>
</View>
</View>
<Divider />
</>
);
}) : false
}
我试过下面的代码
Object.keys 将为您提供对象和映射函数的键 returns 数组格式的键的所有值。
{Object.keys(sortedData).map(key => sortedData[key])}
这可能有帮助
<div>
{arr.map(obj =>
Object.entries(obj).map(([key, value]) => (
<tr key={key}>
<td>Key: {key}</td>
<td>Activity Type: {value.activityType}</td>
<td>DurationInSeconds: {value.durationInSeconds}</td>
</tr>
)))}
</div>