【发布时间】:2021-03-22 12:21:12
【问题描述】:
我想用 array of array 数据格式填充 html 表。我已经编写了代码,但我的数据显示在单行中。我已经在嵌套循环中映射数据。所以我无法正确实施。请提出解决方案。
我的代码:
import React from "react";
import "./App.css";
const data = {
headers: ['Name', 'Country', 'City', 'Mobile', 'Salary', 'Date', 'PAN'],
rows: [
['Maxwell', 'Australia', 'Sydney', '123', '$22', '02/02/89', 'yes'],
['Mark', 'Canada', 'Toronto', '056', '$8965', '12/06/02', 'no'],
['David', 'United Kingdom', 'London', '242', 'S23', '25/02/20', ''],
['Kohli', 'India', 'Delhi', '8956', '$32', '04/12/21', 'yes'],
['ABD', 'RSA', 'captown', '4515', '$32', '2/11/08', null],
],
};
const App = ({ states = {} }) => {
return (
<div>
<table style={{width:"100%"}}>
<tr>
{
data.headers.map((head,i)=>(
<th>{head}</th>
))
}
</tr>
{
data.rows.map((row,j)=>(
row.map((val)=>(
<tr>
<td>{val}</td>
</tr>
))
))
}
</table>
</div>
);
};
export default App;
【问题讨论】:
-
您需要将
<tr>元素移到内部map()调用之外。 -
你有没有在编译后检查 HTML 以查找错误?您使用 .map 是否有特殊原因?为什么不进去?
-
@pilchard 它不是waroking。你能帮我解决吗
标签: javascript arrays reactjs create-react-app