我有一个JSON对象,我正试图使用React将它解析成一个表,但是我很难使用.map()为每一个独特的组合(当然是代码、name、transferable_credits、transferable_credits -> institution、transferable_credits -> name和url )创建行。我似乎不知道如何在每个JSON根键值对中获取嵌套数组的键值。
到目前为止,我的反应成分:
import data from './data.json'
const Home = () => {
return (
<div className="home">
<table>
<thead>
<tr>
<th>Course Code</th>
<th>Vendor Institution</th>
<th>Vendor Course</th>
</tr>
</thead>
<tbody>
{Object.keys(data).map(function(key, index) {
return (
<tr key={index}>
<td>{key}</td>
{Object.keys(data[key].transferable_credits).forEach(function(key2) {
return (
<td>{key2.institution}</td>
)
})}
</tr>
)})
}
</tbody>
</table>
</div>
);
}
export default Home;发布于 2022-01-15 11:00:48
datakey.transferable_credits是一个列表,所以您应该使用map而不是Object.keys循环。尝尝这个
import data from './data.json'
const Home = () => {
return (
<div className="home">
<table>
<thead>
<tr>
<th>Course Code</th>
<th>Vendor Institution</th>
<th>Vendor Course</th>
</tr>
</thead>
<tbody>
{Object.keys(data).map(function(key, index) {
return (
<tr key={index}>
<td>{key}</td>
{data[key].transferable_credits.map(function(key2) {
return (
<td>{key2.institution}</td>
)
})}
</tr>
)})
}
</tbody>
</table>
</div>
);
}
export default Home;https://stackoverflow.com/questions/70720746
复制相似问题