简体   繁体   English

用数组数据的 javascript 数组填充 html 表?

[英]Populate html table with javascript array of array data?

I want to populate the html table with the array of array data format.我想用数组数据格式的数组填充 html 表。 i have write the code but my data is showing in single row.我已经编写了代码,但我的数据显示在单行中。 i have map the data in nested loop.我有 map 嵌套循环中的数据。 so i unable to implement it properly.所以我无法正确实施。 please suggest the solution.请提出解决方案。

my code:我的代码:

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;

You need to move your <tr> element outside of the inner .map() call in order to make a single <tr> per inner array, and a <td> per element of that array.您需要将<tr>元素移到内部.map()调用之外,以便为每个内部数组创建一个<tr> ,并为该数组的每个元素创建一个<td>

{
  data.rows.map((row, j) => (
    <tr>
      {row.map((val) => (
        <td>{val}</td>
      ))}
    </tr>
  ))
}

 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) => ( <tr> {row.map((val) => ( <td>{val}</td> ))} </tr> )) } </table> </div> ); }; ReactDOM.render( <App />, document.getElementById("root") );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> <div id="root"></div>

Just return the <tr> in the outer .map and just the <td> in the inner .map :只需返回外部.map中的<tr>和内部.map中的<td>

{data.rows.map((row) => (
    <tr>
     {row.map((val) => <td>{val}</td>}
    </tr>
))}

This way you will loop values of inner arrays for each entry of the parent array.这样,您将为父数组的每个条目循环内部 arrays 的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM