0
I have the following JSON:
{
"funcionarios":[
{
"id": 0,
"nome": "Marcelo",
"sobrenome": "Silva",
"salario": 3200.00,
"area": "SM"
},
{
"id": 1,
"nome": "Washington",
"sobrenome": "Ramos",
"salario": 2700.00,
"area": "UD"
},
{
"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"
}
]
}
To get the minimum and maximum wage I used the following function:
function sortByAttribute(arr, attribute) {
return arr.sort(function(a, b){
return a[attribute] - b[attribute];
});
In euqe later I store the function return in a array and I withdraw the first index as the lowest salary and the last index being the highest salary. But now I need to calculate the average of wages. How can I do that? I have no idea.
What code have you tried to calculate this average?
– Luiz Felipe
Expensive its function of using the
sort
, so actually you’re ordering, for you to know the minimum or maximum you’ll have to go through your json or your array, which you did to calculate the average ?– Ricardo Lucas
Tip: if you know how to calculate averages and know how to loop in js, the problem is solved. Which of these things you don’t know?
– bfavaretto