create a data table with rainbow colors and some sample data in javascript

Here is an example of how to create a data table with rainbow colors and some sample data in Javascript. The table will have 5 columns and 10 rows.

index.tsx
// Define the data
const data = [
  [1, "John", 20, "Male", 180],
  [2, "Jane", 25, "Female", 165],
  [3, "Bob", 30, "Male", 190],
  [4, "Alice", 35, "Female", 155],
  [5, "Tom", 40, "Male", 175],
  [6, "Emma", 23, "Female", 160],
  [7, "Sam", 28, "Male", 183],
  [8, "Sara", 31, "Female", 170],
  [9, "David", 50, "Male", 195],
  [10, "Lucy", 45, "Female", 150]
];

// Define the rainbow colors
const colors = ["red", "orange", "yellow", "green", "blue", "purple"];

// Get the table element
const table = document.getElementById("my-table");

// Create the table header
const header = table.createTHead();
const row = header.insertRow(0);
for (let i = 0; i < 5; i++) {
  const cell = row.insertCell(i);
  cell.innerHTML = "Column " + (i + 1);
}

// Create the table body
const tbody = table.createTBody();
for (let i = 0; i < 10; i++) {
  const row = tbody.insertRow(i);
  for (let j = 0; j < 5; j++) {
    const cell = row.insertCell(j);
    cell.innerHTML = data[i][j];
    cell.style.backgroundColor = colors[j % colors.length];
  }
}
1040 chars
39 lines

In this example, we first define the sample data in a 2-dimensional array. Then we define an array of rainbow colors to use for the background colors of each column. Next, we get the table element from the HTML document using document.getElementById("my-table"). We create the table header with one row and 5 cells, and insert it into the table using table.createTHead() and header.insertRow(0). We create the table body with 10 rows and 5 cells each, and insert it into the table using table.createTBody() and tbody.insertRow(i). For each cell in the table body, we set the background color to a rainbow color by using colors[j % colors.length], where j is the column index.

gistlibby LogSnag