Rain Man
Rain Man

Reputation: 1263

Merge array of object in js based on spcific values

I have an array of object for classes which includes class status and class time. I want to group the classes based on same start and end time so students can see which classes are closed, which ones are open. For the ones which are open, which ones have the same start and end time.

It should group if all sets of start and end times are the same. So because Math and Gym have the same start and end time, they are group together. Spanish class has 1 set of start and end time as Math and Gym but shouldnt be grouped because it has additional start and end time.

So group them like this:

class_name                              class_status  class_hours
Math - Gym                              Open          [Class Hours]
Science - Art - History - Language      Closed        [No Hours]
Spanish                                 Open          [Class Hours]
Physics                                 Open          [Class Hours]

var list = {
  "time_table": [{
    "class_name": "Math",
    "class_status": "open",
    "class_hrs": [{
      "start": "070000",
      "end": "131400"
    }, {
      "start": "131500",
      "end": "132000"
    }]
  }, {
    "class_name": "Science",
    "class_status": "closed",
    "class_hrs": false
  }, {
    "class_name": "Art",
    "class_status": "closed",
    "class_hrs": false
  }, {
    "class_name": "History",
    "class_status": "closed",
    "class_hrs": false
  }, {
    "class_name": "Language",
    "class_status": "closed",
    "class_hrs": false
  }, {
    "class_name": "Gym",
    "class_status": "open",
    "class_hrs": [{
      "start": "070000",
      "end": "131400"
    }, {
      "start": "131500",
      "end": "132000"
    }]
  },{
    "class_name": "Spanish",
    "class_status": "open",
    "class_hrs": [{
      "start": "070000",
      "end": "131400"
    }, {
      "start": "131500",
      "end": "132000"
    },
    {
      "start": "211500",
      "end": "222000"
    }]
  },
  {
    "class_name": "Physics",
    "class_status": "open",
    "class_hrs": [{
      "start": "080000",
      "end": "131400"
    }, {
      "start": "141500",
      "end": "152000"
    }]
  }]
};

function mergeValues(list) {
  let obj = Object.fromEntries(list.map(({
    class_hrs
  }) => [class_hrs, {
    count: 0,
    class_name: [],
  }]));
  list.forEach(({
    class_hrs,
    class_name,
  }) => {
    obj[class_hrs].count++;
    obj[class_hrs].class_name.push(class_name);
  });
  return obj;
}

console.log(mergeValues(list.time_table));

can someone help me achieve the desired output?

Upvotes: 0

Views: 54

Answers (1)

Best Partner
Best Partner

Reputation: 397

Please try like this.

function mergeValues(list) {
  let obj = Object.fromEntries(list.map(({
    class_hrs
  }) => [JSON.stringify(class_hrs), {
    count: 0,
    class_name: [],
  }]));
  list.forEach(({
    class_hrs,
    class_name,
  }) => {
    obj[JSON.stringify(class_hrs)].count++;
    obj[JSON.stringify(class_hrs)].class_name.push(class_name);
  });
  return obj;
}

If you follow this, you won't lost class_hrs information.

Upvotes: 1

Related Questions