Skip to content

Instantly share code, notes, and snippets.

@sholadedokun
Created January 29, 2019 01:11
Show Gist options
  • Save sholadedokun/acee436b2676f831b92f39a0b4abf97f to your computer and use it in GitHub Desktop.
Save sholadedokun/acee436b2676f831b92f39a0b4abf97f to your computer and use it in GitHub Desktop.
function filterLists(list){
try{
let filteredList=list.filter(function(item, index){
return item.age >= 30 && item.age <= 40
});
let genderIndex=[]
let groupedList=filteredList.reduce(function(accum, item, index){
let gIndex= genderIndex.indexOf(item.gender);
if(gIndex>=0){
accum[gIndex].push(item);
return accum;
}
else{
genderIndex.push(item.gender)
accum.push([item]);
return accum;
}
},[]);
return groupedList
}
catch(e){
return "List must be an array of Object";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment