Сортировка массива по полям с помощью reduce
const fbTeam = [
{
playerName:"Messi",
team:"Barcelona"
},
{
playerName:"Suarez",
team:"Barcelona"
},
{
playerName:"Ronaldo",
team:"Juventus"
},
{
playerName:"Buffon",
team:"Juventus"
},
{
playerName: 'Valerchik',
team: 'Vedrich97'
},
{
playerName:"Gonsalo",
team:"Juventus"
},
{
playerName:"Pedro",
team:"Chelsea"
},
{
playerName:"Zuma",
team:"Chelsea"
},
{
playerName:"Rico",
team:"PSG"
}
];
С помощью метода reduce() преобразуйте его в объект вида:
const fbPlayersByTeams = {
Juventus: [{}, {}, {}],
PSG: [{}],
Chelsea: [{}, {}],
Vedrich97: [{}],
Barcelona: [{}, {}]
}
for (const item of fbTeam) {
if (item.team.includes('Chelsea') == true) fbPlayersByTeams.Chelsea.push(item.playerName)
{
if (item.team.includes('Juventus') == true) fbPlayersByTeams.Juventus.push(item.playerName)
}
{
if (item.team.includes('PSG') == true) fbPlayersByTeams.PSG.push(item.playerName)
}
{
if (item.team.includes('Vedrich97') == true) fbPlayersByTeams.Vedrich97.push(item.playerName)
}
{
if (item.team.includes('Barcelona') == true) fbPlayersByTeams.Barcelona.push(item.playerName)
}
}
console.log(fbPlayersByTeams);
Вот, сделал не с помощью reduce