0

Context: I want to make a timetable planner that checks for time clashes. Any help is greatly appreciated.

Specific Problem: Can't figure out how to split my array of objects into multiple arrays with certain key repeated.

My data set:

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",
      },
    ],
  },
];

Arrays I want to split it into:

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 

**Note:**Dataset is to be populated, ie. users are able to add more courses and timings.

  1. The reason for doing this is so that I can make use of Cartesian Product of arrays to find all combinations.
  2. Then I can check whether there is any time clash in a given combination.
  3. Is there a better way to solve this problem?
Kwan Xhen
  • 5
  • 2
  • 1
    [Array.filter](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) might help you – Max Aug 09 '20 at 08:15

2 Answers2

0

Ciao, you could try to use this example:

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);

I have filtered myCourses array based on course id. Then I made a forEach on slots and for each slot found, I create an object result with course got from array filtered, and the current slot. Finally I pushed this result object in final array called Arrayee3001.

Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30
  • 1
    Code-only answers are discouraged. Please click on [edit] and add some words summarising how your code addresses the question. Thanks – Nick Aug 09 '20 at 08:16
0

Here I am using map to iterate through your array and inside that iteration I am again using map to iterate through the slots.

 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 })
      );
    }
Kwan Xhen
  • 5
  • 2
devd
  • 693
  • 4
  • 11
  • I noticed that using map for first loop through throws an error "Expected to return a value in arrow function." Thus, using forEach for first loop through might be a better solution. – Kwan Xhen Aug 09 '20 at 12:52