Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSON: put object into an array when they have the same date and keep the array with the date in an object in javascript

so i have

  const att = [
    {
      date: '2022-11-04',
      attendance: [{ playerName: 'Play Last', status: 'present' }],
    },
    {
      date: '2022-11-04',
      attendance: [{ playerName: 'New Player Jona', status: 'absent' }],
    },
    {
      date: '2022-11-04',
      attendance: [{ playerName: 'Jona Hysi', status: 'present' }],
    },
    {
      date: '2022-11-04',
      attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }],
    },
    {
      date: '2022-12-04',
      attendance: [{ playerName: 'Play Last', status: 'present' }],
    },
    {
      date: '2022-12-04',
      attendance: [{ playerName: 'New Player Jona', status: 'sick' }],
    },
    {
      date: '2022-12-04',
      attendance: [{ playerName: 'Jona Hysi', status: 'present' }],
    },
    {
      date: '2022-12-04',
      attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }],
    },
    {
      date: '2022-04-14',
      attendance: [{ playerName: 'Play Last', status: 'present' }],
    },
    {
      date: '2022-04-14',
      attendance: [{ playerName: 'New Player Jona', status: 'present' }],
    },
    {
      date: '2022-04-14',
      attendance: [{ playerName: 'Jona Hysi', status: 'present' }],
    },
    {
      date: '2022-04-14',
      attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }],
    },
  ];

and i want to turn it into

  const final = [
    {
      date: '2022-11-04',
      attendance: [
        { playerName: 'Play Last', status: 'present' },
        { playerName: 'New Player Jona', status: 'absent' },
        { playerName: 'Jona Hysi', status: 'present' },
        { playerName: 'Jurgen Tafaj', status: 'present' },
      ],
    },
    {
      date: '2022-12-04',
      attendance: [
        { playerName: 'Play Last', status: 'present' },
        { playerName: 'New Player Jona', status: 'sick' },
        { playerName: 'Jona Hysi', status: 'present' },
        { playerName: 'Jurgen Tafaj', status: 'present' },
      ],
    },
    {
      date: '2022-04-14',
      attendance: [
        { playerName: 'Play Last', status: 'present' },
        { playerName: 'New Player Jona', status: 'present' },
        { playerName: 'Jona Hysi', status: 'present' },
        { playerName: 'Jurgen Tafaj', status: 'present' },
      ],
    },
  ];

so i want to push the object in an array when they have the same date and end up with the date and all their attendance for that date

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i tried: Merge objects in an array if they have the same date but that wasnt with an array with the object and when they have the same name (i have attendance for every object and the example had different names)

i set up a workplace here : https://stackblitz.com/edit/react-vu27ry

>Solution :

Using Array#reduce, iterate over the array while updating a Map where the key is the date and the value is the attendance array. In the end, the map values will be the list of grouped objects

const arr = [
  {
    date: '2022-11-04',
    attendance: [{ playerName: 'Play Last', status: 'present' }]
  },
  {
    date: '2022-11-04',
    attendance: [{ playerName: 'New Player Jona', status: 'absent' }]
  },
  {
    date: '2022-11-04',
    attendance: [{ playerName: 'Jona Hysi', status: 'present' }]
  },
  {
    date: '2022-11-04',
    attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }]
  },
  {
    date: '2022-12-04',
    attendance: [{ playerName: 'Play Last', status: 'present' }]
  },
  {
    date: '2022-12-04',
    attendance: [{ playerName: 'New Player Jona', status: 'sick' }]
  },
  {
    date: '2022-12-04',
    attendance: [{ playerName: 'Jona Hysi', status: 'present' }]
  },
  {
    date: '2022-12-04',
    attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }]
  },
  {
    date: '2022-04-14',
    attendance: [{ playerName: 'Play Last', status: 'present' }]
  },
  {
    date: '2022-04-14',
    attendance: [{ playerName: 'New Player Jona', status: 'present' }]
  },
  {
    date: '2022-04-14',
    attendance: [{ playerName: 'Jona Hysi', status: 'present' }]
  },
  {
    date: '2022-04-14',
    attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }]
  }
];

const res = [...
  arr.reduce((map, { date, attendance = [] }) => {
    const { attendance: dateAttendance = [] } = map.get(date) ?? {};
    map.set(date, { date, attendance: [...dateAttendance, ...attendance] });
    return map;
  }, new Map)
  .values()
];

console.log(res);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading