Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Group item by date

const data = [
  {notes: 'Game was played', time: '2017-10-04T20:24:30+00:00', sport: 'hockey', owner: 'steve', players: '10', game_id: 1},
  { notes: 'Game was played', time: '2017-10-04T12:35:30+00:00', sport: 'lacrosse', owner: 'steve', players: '6', game_id: 2 },
  { notes: 'Game was played', time: '2017-10-14T20:32:30+00:00', sport: 'hockey', owner: 'steve', players: '4', game_id: 3 },
  { notes: 'Game was played', time: '2017-10-04T10:12:30+00:00', sport: 'hockey', owner: 'henry', players: '10', game_id: 4 },
  { notes: 'Game was played', time: '2017-10-14T20:34:30+00:00', sport: 'soccer', owner: 'john', players: '12', game_id: 5 }
];

// this gives an object with dates as keys
const groups = data.reduce((groups, game) => {
  const date = game.time.split('T')[0];
  if (!groups[date]) {
    groups[date] = [];
  }
  groups[date].push(game);
  return groups;
}, {});

// Edit: to add it in the array format instead
const groupArrays = Object.keys(groups).map((date) => {
  return {
    date,
    games: groups[date]
  };
});

console.log(groupArrays);
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript array slice() example 
Javascript :: set get variable in url 
Javascript :: toast not showing 
Javascript :: bottom navigation bar react native hide on keyboard 
Javascript :: crud in node 
Javascript :: remove node from linked list c 
Javascript :: dynamic useState in react 
Javascript :: react props change 
Javascript :: Remove all falsy values from an array 
Javascript :: javascript append array to end of array 
Javascript :: what is prototype in javascript 
Javascript :: object initializer in javascript 
Javascript :: js detect end of array 
Javascript :: how to add external link in angular 
Javascript :: vscode react snippets 
Javascript :: prisma database example 
Javascript :: events 
Javascript :: react-drag-drop-files open twice 
Javascript :: interval manage for javascript 
Javascript :: nodejs date add days 
Javascript :: fibonacci series javascript using recursion explanation 
Javascript :: update data in sequelize 
Javascript :: react native modal 
Javascript :: fastify 
Javascript :: javascript ajax post send an object 
Javascript :: sweet alert 2 
Javascript :: pm2 change log timestamp 
Javascript :: add new element by index js 
Javascript :: Sha256 decrypt javascript 
Javascript :: how to make a quiz in javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =