Menampilkan Array Objek dalam Bentuk Tabel HTML di React

Misalkan kita kembali memiliki array produk kita:

jsxconst prods = [
  {id: 1, name: 'product1', cost: 100},
  {id: 2, name: 'product2', cost: 200},
  {id: 3, name: 'product3', cost: 300},
];

Mari kita tampilkan elemen array kita dalam bentuk tabel HTML:

jsxfunction App() {
  const rows = prods.map(function(item) {
    return <tr key={item.id}>
      <td>{item.name}</td>
      <td>{item.cost}</td>
    </tr>;
  });
  
  return <table>
    <tbody>
      {rows}
    </tbody>
  </table>;
}

Mari tambahkan header kolom untuk tabel kita:

jsxfunction App() {
  const rows = prods.map(function(item) {
    return <tr key={item.id}>
      <td>{item.name}</td>
      <td>{item.cost}</td>
    </tr>;
  });
  
  return <table>
    <thead>
      <tr>
        <td>nama</td>
        <td>harga</td>
      </tr>
    </thead>
    <tbody>
      {rows}
    </tbody>
  </table>;
}
Задание 1

Dalam komponen App diberikan array berikut:

jsxconst users = [
  {id: 1, name: 'user1', surn: 'surn1', age: 30},
  {id: 2, name: 'user2', surn: 'surn2', age: 31},
  {id: 3, name: 'user3', surn: 'surn3', age: 32},
];

Tampilkan elemen array ini dalam bentuk tabel table sehingga setiap bidang objek masuk ke dalam tag td-nya sendiri. Buatlah header kolom untuk tabel Anda.