こんな感じでしょうか。
js
1const data = [
2 {
3 "col1": "a1",
4 "col2": "a2",
5 "col3": "a3",
6 "col4": "a4"
7 },
8 {
9 "col1": "b1",
10 "col2": "b2",
11 "col4": "b4",
12 "col5": "b5"
13 },
14 {
15 "col1": "c1",
16 "col3": "c3",
17 "col4": "c4",
18 "col6": "c6"
19 },
20 {
21 "col2": "d2",
22 "col3": "d3",
23 "col5": "d5",
24 "col6": "d6"
25 },
26 {
27 "col3": "e3",
28 "col4": "e4",
29 "col7": "e7",
30 "col8": "e8"
31 }
32];
33
34function App() {
35 const keys = data.reduce(
36 (keys, obj) => [...keys, ...Object.keys(obj).filter(key => !keys.includes(key))],
37 []);
38
39 return (
40 <>
41 <style>
42 {`
43 th, td { border: 1px solid #ddd; padding: 8px; text-align: center; }
44 thead tr { background: #f5f5f5; }
45 tbody tr:nth-child(odd) { background: #fff; }
46 tbody tr:nth-child(even) { background: #fbfbfb; }
47 `}
48 </style>
49 <table>
50 <thead>
51 <tr>
52 {keys.map(key => <th key={key}>{key}</th>)}
53 </tr>
54 </thead>
55 <tbody>
56 {data.map((obj, index) => (
57 <tr key={index}>
58 {keys.map(key => <td key={key}>{obj[key]}</td>)}
59 </tr>
60 ))}
61 </tbody>
62 </table>
63 </>
64 );
65}
表示例
col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8 |
---|
a1 | a2 | a3 | a4 | | | | |
b1 | b2 | b4 | b5 | | | | |
c1 | c3 | c4 | c6 | | | | |
| d2 | d3 | d5 | d6 | | | |
| e3 | e4 | | e7 | e8 | | |
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/24 14:35
2021/02/25 15:14
2021/02/25 15:35
2021/02/25 15:38
2021/02/25 15:44
2021/02/25 15:49