将三个数组连接成一个数组,该数组是行的笛卡尔乘积与 cols 和与单元格的动态关系的组合

Join three arrays to make one array which is a combination of a Cartesian product of rows with cols and dynamic relation with cells

开发人员;我有三个 arrays:天数()、主题()、数据(单元格)

let Days = ["Monday", "Tuesday", "Wednesday"];
let Subject = [
  "Economics",
  "geography",
  "theatre",
  "music",
  "mathematics",
  "psychology",
  "marketing",
  "business",
  "journalism",
  "languages",
];
const Data = [
  ["10:00", "12:10", "13:30"],
  ["08:30", "12:10", "14:30"],
  ["14:10", "15:15", "19:10"],
  ["20:20", "20:50", "23:00"],
  ["09:10", "10:00", "19:00"],
  ["10:12", "16:40", "18:10"],
  ["08:30", "12:10", "14:30"],
  ["14:10", "15:15", "19:10"],
  ["20:20", "20:50", "23:00"],
  ["08:10", "14:20", "20:20"],
];

我想在这些数组之间建立关系:

我已经尝试过一些类似 modulo 和循环遍历 行和单元格的乘积 :

const cols = Days.length;
const rows = Subject.length;
const len = Subject.length * Days.length;
const table = [];
for (let i = 0, j = 0; i < len; i++) {
  table.push([
    Subject[i % rows],
    Days[j % cols],
    Data[i % rows][j % cols],
  ]);
  j++;
}
console.table(table);

但它给出的结果不是预期的顺序:

我仍在尝试对它进行排序,但无法正常工作,因为排序会按字母顺序排序,而这不符合顺序?

console.table(table.sort((a, b) => a[0].localeCompare(b[0])));

在此先致谢,欢迎和赞赏任何建议

你可以很容易地通过 lop 两次,行和列。

let Days = ["Monday", "Tuesday", "Wednesday"];
let Subject = [
  "Economics",
  "geography",
  "theatre",
  "music",
  "mathematics",
  "psychology",
  "marketing",
  "business",
  "journalism",
  "languages",
];
const Data = [
  ["10:00", "12:10", "13:30"],
  ["08:30", "12:10", "14:30"],
  ["14:10", "15:15", "19:10"],
  ["20:20", "20:50", "23:00"],
  ["09:10", "10:00", "19:00"],
  ["10:12", "16:40", "18:10"],
  ["08:30", "12:10", "14:30"],
  ["14:10", "15:15", "19:10"],
  ["20:20", "20:50", "23:00"],
  ["08:10", "14:20", "20:20"],
];

const cols = Days.length;
const rows = Subject.length;
const len = Subject.length * Days.length;
const table = [];
for (let i = 0; i < rows; i++) {
  for (let j = 0; j < cols; j++) {
    table.push([
      Subject[i],
      Days[j],
      Data[i][j],
    ]);
  }
}
console.table(table);

简单的边做产品边排序,如下:

for (let i = 0; i < rows; i++) {
  for (let j = 0; j < cols; j++) {
      table.push([
      Subject[I],
      Days[j],
      Data[i][j],
      ]);
   }
 }

在这种情况下,您将使用 Days 中的所有相应值迭代 Subject 的每一行,然后移动到 Subject 的下一行,以所需的元素顺序结束。

For example:
  the first iteration will be as follows (representing the first three row of the resulting array)
     i = 0  (Subject[i] = "Economics")
       j = 0  (Days[j] = "Monday") ---> Data[i][j] = "10:00"   
       j = 1  (Days[j] = "Tuesday") ---> Data[i][j] = "12:10"
       j = 2  (Days[j] = "Wednesday") ---> Data[i][j] = "13:30"