Sort by quantity properties with different javascript names

Asked

Viewed 143 times

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?

  • In ascending or descending order

1 answer

2


There is no way to reorder Javascript object properties. What you can do is use the method .sort() to create an ordered array and then create a new object in the order you want.

See a user’s comment on Soen in a similar question to his:

"The order of object properties is not standard in Ecmascript. You should never make assumptions about the order of the elements in an object Javascript. An object is an unordered collection of properties. The answers below show how to 'use' classified properties, using the help of matrices, but never changing the order of properties of the objects themselves. So, no, it’s not possible. Even if you create an object with predefined properties, it is not guaranteed that they be displayed in the same order in the future."

For example, your code returns an object areasContadas thus:

{SM: 3, SD: 2}

To sort from the lowest value to the highest using .sort(), will result in an array like this:

['SD', 'SM']

From this array you can create a new object like this:

{SD: 2, SM: 3} // do menor para o maior

Similarly you can create an object in reverse order.

In the example below I added more objects in the array with 5 keys XX to better illustrate the ordering:

Behold:

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"
},
{
    "id":4,
    "nome":"Cleverton",
    "sobrenome":"Farias",
    "salario":2750.00,
    "area":"XX"
},
{
    "id":4,
    "nome":"Cleverton",
    "sobrenome":"Farias",
    "salario":2750.00,
    "area":"XX"
},
{
    "id":4,
    "nome":"Cleverton",
    "sobrenome":"Farias",
    "salario":2750.00,
    "area":"XX"
},
{
    "id":4,
    "nome":"Cleverton",
    "sobrenome":"Farias",
    "salario":2750.00,
    "area":"XX"
},
{
    "id":4,
    "nome":"Cleverton",
    "sobrenome":"Farias",
    "salario":2750.00,
    "area":"XX"
}
]

let areasContadas = funcionarios.reduce( (todasAreas, areas)=>{
    if(areas.area in todasAreas){
        todasAreas[areas.area]++;
    }else {
        todasAreas[areas.area] = 1;
    }
    return todasAreas;
}, {})

console.log("Sem ordenação:", areasContadas);

const oMenor = Object.keys(areasContadas).sort(function(a,b){return areasContadas[a]-areasContadas[b]})
const menor = {};

oMenor.forEach(function(i){
   menor[i] = areasContadas[i];
});
console.log("Ordenado pelo menor:", menor);

const oMaior = Object.keys(areasContadas).sort(function(a,b){return areasContadas[b]-areasContadas[a]})
const maior = {};

oMaior.forEach(function(i){
   maior[i] = areasContadas[i];
});
console.log("Ordenado pelo maior:", maior);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.