0
Suppose I have the following object array:
const funcionarios = [
{
"id":0,
"nome":"Marcelo",
"sobrenome":"Silva",
"salario":3200.00,
"area":"SM"
},
{
"id":1,
"nome":"Washington",
"sobrenome":"Ramos",
"salario":2700.00,
"area":"SM"
},
{
"id":2,
"nome":"Sergio",
"sobrenome":"Pinheiro",
"salario":2450.00,
"area":"SD"
},
{
"id":3,
"nome":"Bernardo",
"sobrenome":"Costa",
"salario":3700.00,
"area":"SM"
},
{
"id":4,
"nome":"Cleverton",
"sobrenome":"Farias",
"salario":2750.00,
"area":"SD"
}
]
I need to return to the area with the highest number of employees and the area with the lowest number. I was able to count the number of employees in the areas as follows:
let areasContadas = funcionarios.reduce( (todasAreas, areas)=>{
if(areas.area in todasAreas){
todasAreas[areas.area]++;
}else {
todasAreas[areas.area] = 1;
}
return todasAreas;
}, {})
My areasContadas variable now becomes an object with the properties "SM" and "SD" and the number of employees in its values:
areasContadas = {
SM : 3,
SD : 2
}
Now how can I sort and distinguish which area has the most employees and which has the least, where even if I add new areas the process is still functional?
As so ordain?
– Sam
In ascending or descending order
– Marcelo Santos