Get maximum, average and minimum value on a JSON of an object array

Asked

Viewed 707 times

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?

  • 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 ?

  • 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?

2 answers

3


You can wear a bow for to add up all wages and then divide that total by the number of records. So:

const lista = [
  { nome: 'Marcelo', salario: 3200.0 },
  { nome: 'Washington', salario: 2700.0 },
  { nome: 'Sergio', salario: 2450.0 },
  { nome: 'Bernardo', salario: 3700.0 },
  { nome: 'Cleverton', salario: 2750.0 }
];

let total = 0;

for (const item of lista) {
  total += item.salario;
}

const media = total / lista.length;

console.log(media);

If you prefer a slightly more declarative option, you can use the reduce also:

const lista = [
  { nome: 'Marcelo', salario: 3200.0 },
  { nome: 'Washington', salario: 2700.0 },
  { nome: 'Sergio', salario: 2450.0 },
  { nome: 'Bernardo', salario: 3700.0 },
  { nome: 'Cleverton', salario: 2750.0 }
];

const total = lista.reduce((total, item) => total + item.salario, 0);
const media = total / lista.length;

console.log(media);

0

function media(array){
    var somaDosSalarios = 0;

  array.forEach( item => somaDosSalarios+=item.salario);

  return somaDosSalarios / array.length;
}


console.log(media(json.funcionarios));

Browser other questions tagged

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