Considering the array
const a1 = [
{ id: 1, nome: 'Ruan', status: { id: 1, posicao: 'goleiro' } },
{ id: 2, nome: 'Gleison', status: { id: 2, posicao: 'zagueiro' } },
{ id: 3, nome: 'Geraldo', status: { id: 2, posicao: 'zagueiro' } },
{ id: 4, nome: 'Heleno', status: { id: 3, posicao: 'atacante' } },
{ id: 5, nome: 'Djandel', status: { id: 3, posicao: 'atacante' } }
]
I've tried using reduce, but without success, I tried the code below
var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
groupBy(a1, 'status')
};
I also tried with lodash
_.groupBy(a1, 'status');
I hope that 3 different arrays will return, one with the goleiros, another with the zagueiros and another with the atacantes
And how to display the information separately in the react view?