-2

I have an object array in js in the format of :

let arr = [{name:'bill',date:'2021-04-28T00:00:00'} , {name:'nick',date:'1999-12-02T00:00:00'},
           {name:'john',date:'2021-04-28T00:00:00'} , {name:'patrick',date:'1999-12- 02T00:00:00'} ];

I want to create a new array from the one above that groups my objects into arrays where the date is the same . Example for above :

let grouped = [ [ {name:'bill','date':'2021-04-28T00:00:00'} , 
                 {name:'john','date':'2021-04-28T00:00:00'} ] , 
                [
                   {name:'nick','date':'1999-12-02T00:00:00'},
                   {name:'patrick','date':'1999-12-02T00:00:00'}
                ] 
              ]; 

My code below works for small datasets for like 2 , 4 objects but overrides data for more and creates duplicate arrays .

const groupByDates = (array)=>{
  
  let grouped = [];
 
  for(let i=0;i<array.length;i++){
      
      grouped.push([array[i]]); //create [] and push element 
      for(let j=i+1;j<array.length;j++){
        if(array[j].date===array[i].date){ 
          grouped[i].push(array[j]); // if element has same date push to array 
          let index = array.indexOf(array[j].date);
          array.splice(index,1); //remove item from array 
        }
      } 
      //remove item from array after iteration 
      let index = array.indexOf(array[i].date);
      array.splice(index,1);
  }
  return grouped;
}




let arr = [{name:'bill',date:'2021-04-28T00:00:00'} 
            , {name:'nick',date:'1999-12-02T00:00:00'},
              {name:'john',date:'2021-04-28T00:00:00'} , 
              {name:'patrick',date:'1999-12- 02T00:00:00'}, 
              {name:'bruce',date:'1999-12- 02T00:00:00'} ,
              {name:'mary',date:'1999-12- 02T00:00:00'} ,
          
            ];


let group = groupByDates(arr);

console.log(group);

I would appreciate your help

Vasilis Skentos
  • 506
  • 11
  • 22

3 Answers3

1

Here's one way...

let arr = [{name:'bill',date:'2021-04-28T00:00:00'} , {name:'nick',date:'1999-12-02T00:00:00'},
           {name:'john',date:'2021-04-28T00:00:00'} , {name:'patrick',date:'1999-12- 02T00:00:00'} ];


let tmpObj = {}
arr.forEach(item => {
//convert date to an acceptable identifier
var d = "D_" + item.date.replace(/[^a-zA-Z0-9]/g, "");
if (tmpObj[d]) {
  tmpObj[d].push(item);
} else {
  tmpObj[d] = [item]
}
});

let finalarr=[]
for(var x in tmpObj) finalarr.push(tmpObj[x])

console.log(finalarr)
Kinglish
  • 23,358
  • 3
  • 22
  • 43
1

You can use javascript objects and use the dates as keys.

const arr = [{
    name: 'bill',
    date: '2021-04-28T00:00:00'
  },
  {
    name: 'nick',
    date: '1999-12-02T00:00:00'
  },
  {
    name: 'john',
    date: '2021-04-28T00:00:00'
  },
  {
    name: 'patrick',
    date: '1999-12-02T00:00:00'
  }
];

const map = {};

arr.forEach(ele => {
  if (map[ele.date]) map[ele.date].push(ele);
  else map[ele.date] = [ele];
});

const grouped = Object.values(map);

console.log(grouped);
0

You can use Array#reduce with an object to store the values for each date.

let arr = [{name:'bill',date:'2021-04-28T00:00:00'} , {name:'nick',date:'1999-12-02T00:00:00'},
           {name:'john',date:'2021-04-28T00:00:00'} , {name:'patrick',date:'1999-12-02T00:00:00'} ];
let res = Object.values(arr.reduce((acc, curr) => (
  (acc[curr.date] ??= []).push(curr), acc
), {}));
console.log(res);
.as-console-wrapper{max-height:100%!important;top:0}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80