Lưu dữ liệu từ bảng trên trang web vào mảng 2 chiều

Do để thuận tiện cho các thao tác tiếp theo mình muốn lấy dữ liệu của bảng này JavaScript rồi lưu vào một mảng 2 chiều trên JavaScript luôn thay vì dùng Extract Structure Table, mọi người cho mình hỏi có cách nào không? Mình xin cảm ơn ạ

1 Like

Mình gợi ý bạn có thể sử dụng dạng dữ liệu json thay cho mảng 2 chiều nhé, và trả json này về akaBot để tiếp tục xử lí, đây là code ví dụ lấy 1 table đơn giản:

// Get a reference to the table
const table = document.getElementById("myTable");

// Initialize an empty array to store the JSON data
const data = [];

// Loop through the table rows (skipping the header row)
for (let i = 1; i < table.rows.length; i++) {
  const row = table.rows[i];
  const rowData = {};

  // Loop through the table cells
  for (let j = 0; j < row.cells.length; j++) {
    const cell = row.cells[j];
    const headerText = table.rows[0].cells[j].textContent;
    rowData[headerText] = cell.textContent;
  }

  // Push the rowData object into the data array
  data.push(rowData);
}

// Convert the data array to JSON
const jsonData = JSON.stringify(data);

// Display the JSON data
console.log(jsonData);