Skip to content

Instantly share code, notes, and snippets.

@edujr1
Last active May 3, 2019 19:04
Show Gist options
  • Save edujr1/7cd284a27a558934c9354e06559190fd to your computer and use it in GitHub Desktop.
Save edujr1/7cd284a27a558934c9354e06559190fd to your computer and use it in GitHub Desktop.
Fazendo um GroupBy com ES5
function groupListBy(lista, key){
var camposAgrupados = []
lista.forEach(function(e){
if(!camposAgrupados.includes(e[key])){
camposAgrupados.push(e[key])
}
})
var listaFinal = []
camposAgrupados.forEach(function(campo){
listaFinal.push(
{
[key]:campo,
lista:lista.filter(function(itemList){
return itemList[key] == campo
})
}
)
})
return listaFinal
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment