Home > Back-end >  how to merge array object and delete duplicate item use javascript
how to merge array object and delete duplicate item use javascript

Time:07-05

This is my data,

I need to group this data by type and reverse_course_id and name, if the type has the same teacher_name and same start

Is that possible?

Input data:

let data = [
  {
    class_room: "",
    color: "#FEE6C9",
    name: "classA_speak",
    reserve_course_id: 4814,
    start: "2022-07-07 15:00",
    start_time: "15:00",
    teacher_name: "airel",
    type: 1,
  },
  {
    class_room: "",
    color: "#FEE6C9",
    name: "classA_write",
    reserve_course_id: 4815,
    start: "2022-07-07 15:00",
    start_time: "15:00",
    teacher_name: "airel",
    type: 1
  },
  {
    class_room: "",
    color: "#FEE6C9",
    name: "classB_speak",
    reserve_course_id: 4816,
    start: "2022-07-07 15:00",
    start_time: "15:00",
    teacher_name: "airel",
    type: 1
  },
    {
    class_room: "",
    color: "#D2F0FF",
    name: "classB_write",
    reserve_course_id: 4877,
    start: "2022-07-08 15:00",
    start_time: "15:00",
    teacher_name: "Ian",
    type: 2
  },
    {
    class_room: "",
    color: "#FEE6C9",
    name: "classB_speak",
    reserve_course_id: 4919,
    start: "2022-07-08 18:00",
    start_time: "18:00",
    teacher_name: "Tom",
    type: 1
  },
    {
    class_room: "",
    color: "#FEE6C9",
    name: "classB_write",
    reserve_course_id: 4911,
    start: "2022-07-08 18:00",
    start_time: "18:00",
    teacher_name: "Tom",
    type: 1
  },
    {
    class_room: "",
    color: "#FEE6C9",
    name: "classA_speak",
    reserve_course_id: 4912,
    start: "2022-07-08 18:00",
    start_time: "18:00",
    teacher_name: "Tom",
    type: 1
  },
    {
    class_room: "",
    color: "#D8D8D8",
    name: "classB_speak",
    reserve_course_id: 4933,
    start: "2022-07-03 11:00",
    start_time: "11:00",
    teacher_name: "airel",
    type: 3
  },
    {
    class_room: "",
    color: "#FFD9D9",
    name: "classB_write",
    reserve_course_id: 4944,
    start: "2022-07-10 09:00",
    start_time: "09:00",
    teacher_name: "Ian",
    type: 4
  },
      {
    class_room: "",
    color: "#FFD9D9",
    name: "classB_speak",
    reserve_course_id: 4899,
    start: "2022-07-20 15:00",
    start_time: "15:00",
    teacher_name: "Ian",
    type: 4
  },
    {
    class_room: "",
    color: "#FEE6C9",
    name: "classB_speak",
    reserve_course_id: 5000,
    start: "2022-07-13 11:00",
    start_time: "11:00",
    teacher_name: "Ian",
    type: 1
  },
    {
    class_room: "",
    color: "#FEE6C9",
    name: "classB_write",
    reserve_course_id: 5001,
    start: "2022-07-13 11:00",
    start_time: "11:00",
    teacher_name: "Ian",
    type: 1
  },
    {
    class_room: "",
    color: "#FEE6C9",
    name: "classA_speak",
    reserve_course_id: 5002,
    start: "2022-07-13 11:00",
    start_time: "11:00",
    teacher_name: "Ian",
    type: 1
  },
    {
    class_room: "",
    color: "#FEE6C9",
    name: "classA_write",
    reserve_course_id: 5003,
    start: "2022-07-13 11:00",
    start_time: "11:00",
    teacher_name: "Ian",
    type: 1
  },
]

Ouput data:

let result = [
  {
    class_room: "",
    color: "#FEE6C9",
    name: ["classA_speak","classA_write","classB_speak"],
    reserve_course_id: [4814,4815,4816],
    start: "2022-07-07 15:00",
    start_time: "15:00",
    teacher_name: "airel",
    type: 1,
  },
  {
    class_room: "",
    color: "#D2F0FF",
    name: "classB_write",
    reserve_course_id: 4877,
    start: "2022-07-08 15:00",
    start_time: "15:00",
    teacher_name: "Ian",
    type: 2
  },
  {
    class_room: "",
    color: "#FEE6C9",
    name: ["classB_speak", "classB_write","classA_speak"],
    reserve_course_id: [4910,4911,4912],
    start: "2022-07-08 18:00",
    start_time: "18:00",
    teacher_name: "Tom",
    type: 1
  },
  {
    class_room: "",
    color: "#D8D8D8",
    name: "classB_speak",
    reserve_course_id: 4933,
    start: "2022-07-03 11:00",
    start_time: "11:00",
    teacher_name: "airel",
    type: 3
  },
  {
    class_room: "",
    color: "#FFD9D9",
    name: "classB_write",
    reserve_course_id: 4944,
    start: "2022-07-10 09:00",
    start_time: "09:00",
    teacher_name: "Ian",
    type: 4
  },
  {
    class_room: "",
    color: "#FFD9D9",
    name: "classB_speak",
    reserve_course_id: 4899,
    start: "2022-07-20 15:00",
    start_time: "15:00",
    teacher_name: "Ian",
    type: 4
  },
  {
    class_room: "",
    color: "#FEE6C9",
    name: ["classB_speak","classA_write","classA_speak","classA_write"],
    reserve_course_id: [5000,5001,5002,5003],
    start: "2022-07-13 11:00",
    start_time: "11:00",
    teacher_name: "Ian",
    type: 1
  },
]

Thank you!

CodePudding user response:

You can use Array.prototype.reduce() combined with Logical nullish assignment (??=) to create an object where the properties are: ${c.type}-${c.teacher_name} and finally extract the array values with Object.values()

Code:

const data = [{class_room: '',color: '#FEE6C9',name: 'classA_speak',reserve_course_id: 4814,start: '2022-07-07 15:00',start_time: '15:00',teacher_name: 'airel',type: 1,},{class_room: '',color: '#FEE6C9',name: 'classA_write',reserve_course_id: 4815,start: '2022-07-07 15:00',start_time: '15:00',teacher_name: 'airel',type: 1,},{class_room: '',color: '#FEE6C9',name: 'classB_speak',reserve_course_id: 4816,start: '2022-07-07 15:00',start_time: '15:00',teacher_name: 'airel',type: 1,},{class_room: '',color: '#D2F0FF',name: 'classB_write',reserve_course_id: 4877,start: '2022-07-08 15:00',start_time: '15:00',teacher_name: 'Ian',type: 2,},{class_room: '',color: '#FEE6C9',name: 'classB_speak',reserve_course_id: 4919,start: '2022-07-08 18:00',start_time: '18:00',teacher_name: 'Tom',type: 1,},{class_room: '',color: '#FEE6C9',name: 'classB_write',reserve_course_id: 4911,start: '2022-07-08 18:00',start_time: '18:00',teacher_name: 'Tom',type: 1,},{class_room: '',color: '#FEE6C9',name: 'classA_speak',reserve_course_id: 4912,start: '2022-07-08 18:00',start_time: '18:00',teacher_name: 'Tom',type: 1,},{class_room: '',color: '#D8D8D8',name: 'classB_speak',reserve_course_id: 4933,start: '2022-07-03 11:00',start_time: '11:00',teacher_name: 'airel',type: 3,},{class_room: '',color: '#FFD9D9',name: 'classB_write',reserve_course_id: 4944,start: '2022-07-10 09:00',start_time: '09:00',teacher_name: 'Ian',type: 4,},{class_room: '',color: '#FFD9D9',name: 'classB_speak',reserve_course_id: 4899,start: '2022-07-20 15:00',start_time: '15:00',teacher_name: 'Ian',type: 4,},{class_room: '',color: '#FEE6C9',name: 'classB_speak',reserve_course_id: 5000,start: '2022-07-13 11:00',start_time: '11:00',teacher_name: 'Ian',type: 1,},{class_room: '',color: '#FEE6C9',name: 'classB_write',reserve_course_id: 5001,start: '2022-07-13 11:00',start_time: '11:00',teacher_name: 'Ian',type: 1,},{class_room: '',color: '#FEE6C9',name: 'classA_speak',reserve_course_id: 5002,start: '2022-07-13 11:00',start_time: '11:00',teacher_name: 'Ian',type: 1,},{class_room: '',color: '#FEE6C9',name: 'classA_write',reserve_course_id: 5003,start: '2022-07-13 11:00',start_time: '11:00',teacher_name: 'Ian',type: 1,}]

const result = Object.values(
  data.reduce((a, c) => {
    const key = `${c.type}-${c.teacher_name}`
    a[key] ??= { ...c, name: [], reserve_course_id: [] }
    a[key].name.push(c.name)
    a[key].reserve_course_id.push(c.reserve_course_id)
    return a
  }, {})
)

console.log(result)

CodePudding user response:

I believe so!

Bellow is the answer:

function getGroupedData(data) {
  const hash = {};

  for(let element of data) {
     if(!hash[element.type]) {
        hash[element.type] = element;
        hash[element.type].name = [];
        hash[element.type].reserve_course_id = [];
     }
     // Makes sure that duplicated are not added to the array
     if(!hash[element.type].name.find(name => name === element.name)) {
          hash[element.type].name.push(element.name);
     }
     // Makes sure that duplicated are not added to the array
     if(!hash[element.type].reserve_course_id.find(id => id === element.reserve_course_id)) {
          hash[element.type].reserve_course_id.push(element.reserve_course_id);
     }
  }
  const result = [];
  
  for(let position in hash)
    result.push(hash[position]);
    
  return result;
}

function getData() {
  return [
    {
      class_room: "",
      color: "#FEE6C9",
      name: "classA_speak",
      reserve_course_id: 4814,
      start: "2022-07-07 15:00",
      start_time: "15:00",
      teacher_name: "airel",
      type: 1,
    },
    {
      class_room: "",
      color: "#FEE6C9",
      name: "classA_write",
      reserve_course_id: 4815,
      start: "2022-07-07 15:00",
      start_time: "15:00",
      teacher_name: "airel",
      type: 1
    },
    {
      class_room: "",
      color: "#FEE6C9",
      name: "classB_speak",
      reserve_course_id: 4816,
      start: "2022-07-07 15:00",
      start_time: "15:00",
      teacher_name: "airel",
      type: 1
    },
      {
      class_room: "",
      color: "#D2F0FF",
      name: "classB_write",
      reserve_course_id: 4877,
      start: "2022-07-08 15:00",
      start_time: "15:00",
      teacher_name: "Ian",
      type: 2
    },
      {
      class_room: "",
      color: "#FEE6C9",
      name: "classB_speak",
      reserve_course_id: 4919,
      start: "2022-07-08 18:00",
      start_time: "18:00",
      teacher_name: "Tom",
      type: 1
    },
      {
      class_room: "",
      color: "#FEE6C9",
      name: "classB_write",
      reserve_course_id: 4911,
      start: "2022-07-08 18:00",
      start_time: "18:00",
      teacher_name: "Tom",
      type: 1
    },
      {
      class_room: "",
      color: "#FEE6C9",
      name: "classA_speak",
      reserve_course_id: 4912,
      start: "2022-07-08 18:00",
      start_time: "18:00",
      teacher_name: "Tom",
      type: 1
    },
      {
      class_room: "",
      color: "#D8D8D8",
      name: "classB_speak",
      reserve_course_id: 4933,
      start: "2022-07-03 11:00",
      start_time: "11:00",
      teacher_name: "airel",
      type: 3
    },
      {
      class_room: "",
      color: "#FFD9D9",
      name: "classB_write",
      reserve_course_id: 4944,
      start: "2022-07-10 09:00",
      start_time: "09:00",
      teacher_name: "Ian",
      type: 4
    },
        {
      class_room: "",
      color: "#FFD9D9",
      name: "classB_speak",
      reserve_course_id: 4899,
      start: "2022-07-20 15:00",
      start_time: "15:00",
      teacher_name: "Ian",
      type: 4
    },
      {
      class_room: "",
      color: "#FEE6C9",
      name: "classB_speak",
      reserve_course_id: 5000,
      start: "2022-07-13 11:00",
      start_time: "11:00",
      teacher_name: "Ian",
      type: 1
    },
      {
      class_room: "",
      color: "#FEE6C9",
      name: "classB_write",
      reserve_course_id: 5001,
      start: "2022-07-13 11:00",
      start_time: "11:00",
      teacher_name: "Ian",
      type: 1
    },
      {
      class_room: "",
      color: "#FEE6C9",
      name: "classA_speak",
      reserve_course_id: 5002,
      start: "2022-07-13 11:00",
      start_time: "11:00",
      teacher_name: "Ian",
      type: 1
    },
      {
      class_room: "",
      color: "#FEE6C9",
      name: "classA_write",
      reserve_course_id: 5003,
      start: "2022-07-13 11:00",
      start_time: "11:00",
      teacher_name: "Ian",
      type: 1
    },
  ];
}

console.log(getGroupedData(getData()));

CodePudding user response:

const datas = [{class_room:"",color:"#FEE6C9",leave:"",name:"classA_speak",reserve_course_id:4814,start:"2022-07-07 15:00",start_time:"15:00",teacher_name:"airel",teacher_temporary_leave_id:"",timed:!1,type:1},{class_room:"",color:"#FEE6C9",leave:"",name:"classA_write",reserve_course_id:4815,start:"2022-07-07 15:00",start_time:"15:00",teacher_name:"airel",teacher_temporary_leave_id:"",timed:!1,type:1},{class_room:"",color:"#FEE6C9",leave:"",name:"classB_speak",reserve_course_id:4816,start:"2022-07-07 15:00",start_time:"15:00",teacher_name:"airel",teacher_temporary_leave_id:"",timed:!1,type:1},{class_room:"",color:"#D2F0FF",leave:"",name:"classB_write",reserve_course_id:4877,start:"2022-07-08 15:00",start_time:"15:00",teacher_name:"Ian",teacher_temporary_leave_id:"",timed:!1,type:2},{class_room:"",color:"#FEE6C9",leave:"",name:"classB_speak",reserve_course_id:4919,start:"2022-07-08 18:00",start_time:"18:00",teacher_name:"Tom",teacher_temporary_leave_id:"",timed:!1,type:1},{class_room:"",color:"#FEE6C9",leave:"",name:"classB_write",reserve_course_id:4911,start:"2022-07-08 18:00",start_time:"18:00",teacher_name:"Tom",teacher_temporary_leave_id:"",timed:!1,type:1},{class_room:"",color:"#FEE6C9",leave:"",name:"classA_speak",reserve_course_id:4912,start:"2022-07-08 18:00",start_time:"18:00",teacher_name:"Tom",teacher_temporary_leave_id:"",timed:!1,type:1},{class_room:"",color:"#D8D8D8",leave:"",name:"classB_speak",reserve_course_id:4933,start:"2022-07-03 11:00",start_time:"11:00",teacher_name:"airel",teacher_temporary_leave_id:"",timed:!1,type:3},{class_room:"",color:"#FFD9D9",leave:"",name:"classB_write",reserve_course_id:4944,start:"2022-07-10 09:00",start_time:"09:00",teacher_name:"Ian",teacher_temporary_leave_id:"",timed:!1,type:4},{class_room:"",color:"#FFD9D9",leave:"",name:"classB_speak",reserve_course_id:4899,start:"2022-07-20 15:00",start_time:"15:00",teacher_name:"Ian",teacher_temporary_leave_id:"",timed:!1,type:4},{class_room:"",color:"#FEE6C9",leave:"",name:"classB_speak",reserve_course_id:5e3,start:"2022-07-13 11:00",start_time:"11:00",teacher_name:"Ian",teacher_temporary_leave_id:"",timed:!1,type:1},{class_room:"",color:"#FEE6C9",leave:"",name:"classB_write",reserve_course_id:5001,start:"2022-07-13 11:00",start_time:"11:00",teacher_name:"Ian",teacher_temporary_leave_id:"",timed:!1,type:1},{class_room:"",color:"#FEE6C9",leave:"",name:"classA_speak",reserve_course_id:5002,start:"2022-07-13 11:00",start_time:"11:00",teacher_name:"Ian",teacher_temporary_leave_id:"",timed:!1,type:1},{class_room:"",color:"#FEE6C9",leave:"",name:"classA_write",reserve_course_id:5003,start:"2022-07-13 11:00",start_time:"11:00",teacher_name:"Ian",teacher_temporary_leave_id:"",timed:!1,type:1}];

// get group key
const keys = [...new Set(datas.map(d => `${d.start} - ${d.teacher_name}`))];

// group data
const groups = keys.map(key => datas.filter(d => `${d.start} - ${d.teacher_name}` == key));

// analyze and sort an array of grouped data
const analyze = groups.map(group => {
  const reduceFun = (f) => group.reduce((a, c) => [...a, c[f]], []);
  return {
    ...group[0],
    reserve_course_id: reduceFun('reserve_course_id'),
    name: reduceFun('name')
  }
}).sort((a, b) => a.type - b.type);

console.log(analyze);

  • Related