JavasCript 将一个对象数组拆分为具有特定键的多个数组

JavasSript Split an array of objects into multiple arrays with a certain key

上下文:我想制作一个时间表计划器来检查时间冲突。非常感谢任何帮助。

具体问题:无法弄清楚如何将我的对象数组拆分为多个重复特定键的数组。

我的数据集:

let myCourses = [
  {
    course: "ee3001",
    slots: [
      {
        day: "monday",
        time: "0900-1100",
      },
      {
        day: "tuesday",
        time: "0930-1100",
      },
      {
        day: "wednesday",
        time: "1330-1530",
      },
    ],
  },
  {
    course: "ee3002",
    slots: [
      {
        day: "monday",
        time: "0900-1100",
      },
      {
        day: "thursday",
        time: "0930-1130",
      },
    ],
  },
  {
    course: "ee3003",
    slots: [
      {
        day: "tuesday",
        time: "0930-1100",
      },
      {
        day: "wednesday",
        time: "1330-1530",
      },
      {
        day: "thursday",
        time: "0930-1130",
      },
    ],
  },
];

我要拆分成的数组:

let newarray = [
  {
    course: "ee3001",
    slot: {
      day: "monday",
      time: "0900-1100",
    },
  },
  {
    course: "ee3001",
    slot: {
      day: "monday",
      time: "1300-1400",
    },
  },
  ...
  ...
];

let newArray2 = //containing info on ee3002
let newArray3 = //containing info on ee3003 

**注意:**要填充数据集,即。用户可以添加更多课程和时间。

  1. 这样做的原因是我可以利用 Cartesian Product of arrays 来查找所有组合。
  2. 然后我可以检查给定的组合是否有任何时间冲突。
  3. 有没有更好的方法解决这个问题?

再见,你可以试试这个例子:

let myCourses = [
      {
        course: "ee3001",
        slots: [
          {
            day: "monday",
            time: "0900-1100",
          },
          {
            day: "tuesday",
            time: "0930-1100",
          },
          {
            day: "wednesday",
            time: "1330-1530",
          },
        ],
      },
      {
        course: "ee3002",
        slots: [
          {
            day: "monday",
            time: "0900-1100",
          },
          {
            day: "thursday",
            time: "0930-1130",
          },
        ],
      },
      {
        course: "ee3003",
        slots: [
          {
            day: "tuesday",
            time: "0930-1100",
          },
          {
            day: "wednesday",
            time: "1330-1530",
          },
          {
            day: "thursday",
            time: "0930-1130",
          },
        ],
      },
    ];
    let Arrayee3001 = [];
    let array1 = myCourses.filter(course => course.course === "ee3001")
    array1[0].slots.forEach(slot => {
       let result = {};
       result.course = array1[0].course;
       result.slot = [];
       result.slot.push(slot);
       Arrayee3001.push(result);
    });
    
    console.log(Arrayee3001);

我已经根据 course id 过滤了 myCourses 数组。然后我在 slots 上创建了一个 forEach,对于找到的每个插槽,我创建了一个对象 result,其中 course 从数组过滤得到,当前 slot。最后,我将这个 result 对象推送到名为 Arrayee3001.

的最终数组中

在这里,我使用 map 遍历您的数组,在该迭代中,我再次使用 map 遍历插槽。

 let myCourses = [
      {
        course: "ee3001",
        slots: [
          {
            day: "monday",
            time: "0900-1100",
          },
          {
            day: "tuesday",
            time: "0930-1100",
          },
          {
            day: "wednesday",
            time: "1330-1530",
          },
        ],
      },
      {
        course: "ee3002",
        slots: [
          {
            day: "monday",
            time: "0900-1100",
          },
          {
            day: "thursday",
            time: "0930-1130",
          },
        ],
      },
      {
        course: "ee3003",
        slots: [
          {
            day: "tuesday",
            time: "0930-1100",
          },
          {
            day: "wednesday",
            time: "1330-1530",
          },
          {
            day: "thursday",
            time: "0930-1130",
          },
        ],
      },
    ];
    
    const newArray=[]
    myCourses.forEach(myFunction);

    function myFunction(item, index) {
      newArray[index] = [];
      item.slots.map((child) =>
        newArray[index].push({ course: item.course, slots: child })
      );
    }