/* INSTRUCTIONS: Group an array of objects by status, and return the status name that has the most items in its corresponding array. */
const arr = [
{
id: 1,
title: "target1",
status: "pending"
},
{
id: 2,
title: "target2",
status: "pending"
},
{
id: 3,
title: "target3",
status: "declined"
},
{
id: 4,
title: "target4",
status: "approved"
},
{
id: 5,
title: "target1",
status: "approved"
},
{
id: 6,
title: "target6",
status: "pending"
}
];
function groupTargetsByStatus(arr) {
const group = {}
arr.forEach(item => {
!group[item.status]
? group[item.status]=[ item ]
: group[item.status].push(item)
})
return group
}
function statusThatHasMostTargets(arr) {
// your code here
const group = groupTargetsByStatus(arr)
let max = 0
let maxGroup = ''
for ( let item in group) {
if (item.length > max){
max = item.length
maxGroup = item[0].status
}
}
}
console.log(groupTargetsByStatus(arr))
/* {
pending: [
{
id: 1,
title: "target1",
status: "pending"
},
{
id: 2,
title: "target2",
status: "pending"
},
{
id: 6,
title: "target6",
status: "pending"
}
],
approved: [
{
id: 4,
title: "target4",
status: "approved"
},
{
id: 5,
title: "target1",
status: "approved"
}
],
declined: [
{
id: 3,
title: "target3",
status: "declined"
}
]
} */
console.log(statusThatHasMostTargets(arr))
// "pending"