Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
How to convert multiple JavaScript objects with the same value into one single object that amounts to the sum of those values?

This is my first post, so I will try to do my best to be as thorough as possible! I need to convert multiple JavaScript objects that have a same value into a single unique object with the totals of all other keys in the object from each.

I have an array that contains objects for different schools, and each has a value for the number of students at the school. Here is what I am working with:

let schools = [
  {
    school: "a",
    students: 1000,
  },
  {
    school: "b",
    students: 10000,
  },
  {
    school: "b",
    students: 400,
  },
  {
    school: "c",
    students: 10000,
  },
  {
    school: "d",
    students: 2000,
  },
  {
    school: "c",
    students: 1000,
  },
  {
    school: "d",
    students: 4000,
  },
]

I want to return an array of single objects for each given school, with the sum of values of students in each of the given schools' objects. Here is the result I am expecting:

  {
    school: "a",
    students: 1000,
  },
  {
    school: "b",
    students: 10400,
  },
  {
    school: "c",
    students: 11000,
  },
  {
    school: "d",
    students: 6000,
  }
]

Here is what my solution looks like so far:

let listOfUniqueSchools = [];
for (let i = 0; i < schools.length; i++) {
  if (!listOfUniqueSchools.includes(schools[i].school)) {
    listOfUniqueSchools.push(schools[i].school);
  } else {
  }
}

let outputArray = [];
for (let j = 0; j < listOfUniqueSchools.length; j++) {
  for (let k = 0; k < schools.length; k++) {
    let totalNumberOfStudents = 0;
    if (listOfUniqueSchools[j] == schools[k].school) {
      totalNumberOfStudents += schools[k].students;
      outputArray.push({
        school: schools[k].school,
        students: totalNumberOfStudents,
      });
    }
  }
}

Here is what I am getting:

[
  {
    school: "a",
    students: 1000,
  },
  {
    school: "b",
    students: 10000,
  },
  {
    school: "b",
    students: 400,
  },
  {
    school: "c",
    students: 10000,
  },
  {
    school: "c",
    students: 1000,
  },
  {
    school: "d",
    students: 2000,
  },
 
  {
    school: "d",
    students: 4000,
  },
]

Does anyone know what I need to change to solve this?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This solution uses a for loop (forEach) to populate a new object that has schools as keys and student totals as the values. Then we use the Object.keys method to get an array of schools that are found in the totals object, and we use the map function to transform the data into the expected output format.

It's a lot cleaner than nested loops.

const schools = [{
    school: "a",
    students: 1000,
  },
  {
    school: "b",
    students: 10000,
  },
  {
    school: "b",
    students: 400,
  },
  {
    school: "c",
    students: 10000,
  },
  {
    school: "d",
    students: 2000,
  },
  {
    school: "c",
    students: 1000,
  },
  {
    school: "d",
    students: 4000,
  },
]


const totals = {}
schools.forEach((entry) => {
  if (totals.hasOwnProperty(entry.school)) {
    totals[entry.school] += entry.students
  } else {
    totals[entry.school] = entry.students
  }
})

const formattedTotals = Object.keys(totals).map((school) => ({
  school: school,
  students: totals[school]
}))

console.log(formattedTotals)

about 4 years ago · Juan Pablo Isaza Relatório

0

The problem in your code is the Array.push part is in the inner loop while it should be at outer loop.

outputArray.push({
    school: listOfUniqueSchools[j],
    students: totalNumberOfStudents,
});

Move it outside it will work.

It could be simpler using Array.reduce, see the code snippet below.

let schools = [ {    school: "a",    students: 1000,  },  {    school: "b",    students: 10000,  },{    school: "b",    students: 400,  },  {    school: "c",    students: 10000,  },  {    school: "d",    students: 2000,  },  {    school: "c",    students: 1000,  },  {    school: "d",    students: 4000,  },];


let listOfUniqueSchools = [];
for (let i = 0; i < schools.length; i++) {
    if (!listOfUniqueSchools.includes(schools[i].school)) {
        listOfUniqueSchools.push(schools[i].school);
    } else {
    }
}

let outputArray = [];
for (let j = 0; j < listOfUniqueSchools.length; j++) {
    let totalNumberOfStudents = 0;
    for (let k = 0; k < schools.length; k++) {
        if (listOfUniqueSchools[j] == schools[k].school) {
            totalNumberOfStudents += schools[k].students;
        }
    }
    outputArray.push({
        school: listOfUniqueSchools[j],
        students: totalNumberOfStudents,
    });
}

console.log(outputArray);

// or simply use array reduce

const result = schools.reduce((acc, item) => {
    const sum = acc.find(sum => sum.school === item.school);
    sum ? sum.students += item.students : acc.push({school: item.school, students: item.students});
    return acc;
}, []);

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

I would loop through the schools and create an object where keys are schools ("a", "b", "c", etc.) and values are the cumulative number of students.

Then cycle through that array to create an array of objects, each of which has a key "school" and value, and a key "students" with the cumulative value.

let schools = [
  {
    school: "a",
    students: 1000,
  },
  {
    school: "b",
    students: 10000,
  },
  {
    school: "b",
    students: 400,
  },
  {
    school: "c",
    students: 10000,
  },
  {
    school: "d",
    students: 2000,
  },
  {
    school: "c",
    students: 1000,
  },
  {
    school: "d",
    students: 4000,
  },
]

let totals = {} // Interim totals
schools.forEach( function(school) {
    // console.log(school.students, school.school)
    totals[school.school] = ( totals[school.school] ?? 0 ) + school.students;
})

let results = [] // Final results
for ( const school in totals ) {
    results.push(
        {
            school: school,
            students: totals[school]
        }
    )
}

console.log(results)

/*
Array(4) [ {…}, {…}, {…}, {…} ]
​0: Object { school: "a", students: 1000 }
​1: Object { school: "b", students: 10400 }
​2: Object { school: "c", students: 11000 }
​3: Object { school: "d", students: 6000 }
​length: 4
*/
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda