【发布时间】:2021-07-30 12:06:15
【问题描述】:
我尝试制作获取对象数组并根据数组中的对象生成具有动态行跨度的表的函数。 我尝试了很多解决方案,但都没有帮助。
这段代码我试过了,但没有继续,因为一开始效果不好
const returnTabel = state => {
return state.map((item, index) => {
return (
<tr key={index}>
{Object.keys(item).map((key, index) => {
if (Array.isArray(item[key])) {
return item[key].map((object, index) => {
return Object.keys(object).map((i, index) => {
if (Array.isArray(object[i])) {
} else {
return (
<tr>
<td>{object[i]}</td>
</tr>
);
}
});
});
} else {
return (
<td rowSpan={2} key={index}>
{item[key]}
</td>
);
}
})}
</tr>
);
});};
这是我的数据:
const state = [
{
name: 'Bill',
info: [
{
hobby: 'Practice',
field: [
{ type: 'Swim', hours: '6' },
{ type: 'Run', hours: '7' }
]
},
{
hobby: 'Listen to music',
field: [
{ type: 'Jazz', hours: '3' },
{ type: 'Electronic music', hours: '3' },
{ type: 'Hip hop', hours: '3' }
]
}
],
student: 'No'
},
{
name: 'John',
info: [
{
hobby: 'Practice',
field: [
{ type: 'Swim', hours: '1' },
{ type: 'Run', hours: '2' }
]
}
],
student: 'Yes'
}]
我想用我的数据制作这张表
【问题讨论】:
-
tried many solutions。你能给我们看看吗? -
@Keith 我添加了。但它并没有真正起作用..
-
最好的建议是将其视为 React 之外的数组映射练习。然后,一旦你可以单独在 javascript 中映射它,就将它传递给反应渲染
标签: javascript reactjs html-table