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

100
Visualizações
Creating a reduce method that can handle any number of objects with unique Ids

I am trying to use the reduce method to sort through an array of objects. My goal is to create a reduce method that can handle any number of objects (with unique eventIds). The desired output would be formatted like this but with the capability to handle more than just five unique eventIds

[Desired Output]

     (5) [{…}, {…}, {…}, {…}, {…}]
       0:
          eventId: "12345"
          total: Array(3)
            0: {total: 123}
            1: {total: 45}
            2: {total: 123}
            length: 3

       1: {total: Array(3), eventId: '12347'}
       2: {total: Array(3), eventId: '12349'}
       3: {total: Array(3), eventId: '12348'}
       4: {total: Array(3), eventId: '12346'}

To get the output above I used this reduce method.

[Reduce Method]

      const sortedSalesData = salesData.reduce((acc, obj) => {

      for ( let i = 0; i<salesData.length; i++) {

        if (obj.eventId === salesData[i].eventId ) {

          acc[i].eventId = salesData[i].eventId;
          acc[i].total = [...acc[i].total, {total: obj.total}];

          return acc;
        }
      };
      
  },[{total:[]},{total:[]},{total:[]},{total:[]},{total:[]}]);  

But I'm not sure how to set it up to handle more than those five unique eventIds. Below is the original array for reference.

[Original Array]

    const salesData = 
    [
      { eventId: '12345', total: 123 },
      { eventId: '12347', total: 45 },
      { eventId: '12349', total: 78 },
      { eventId: '12348', total: 123 },
      { eventId: '12346', total: 45 },
      { eventId: '12349', total: 78 },
      { eventId: '12347', total: 123 },
      { eventId: '12345', total: 45 },
      { eventId: '12348', total: 78 },
      { eventId: '12345', total: 123 },
      { eventId: '12347', total: 45 },
      { eventId: '12346', total: 78 },
      { eventId: '12349', total: 123 },
      { eventId: '12348', total: 45 },
      { eventId: '12346', total: 78 },
     ];     

The hangup is getting it to a place where it can handle any number of unique events without having to add an additional {total:[]} at the end of the method.

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

You wan t to iterate over the initial array (obj), insert the iterated element on a new array (acc) if eventId doesn't exist yet on said array, when it does exist (accObj), you want to push the total object of the current element (obj) onto the total array of the new element (accObj)?

I think this does what you need:

let array = [
  { eventId: '12345', total: 123 },
  { eventId: '12347', total: 45 },
  { eventId: '12349', total: 78 },
  { eventId: '12348', total: 123 },
  { eventId: '12346', total: 45 },
  { eventId: '12349', total: 78 },
  { eventId: '12347', total: 123 },
  { eventId: '12345', total: 45 },
  { eventId: '12348', total: 78 },
  { eventId: '12345', total: 123 },
  { eventId: '12347', total: 45 },
  { eventId: '12346', total: 78 },
  { eventId: '12349', total: 123 },
  { eventId: '12348', total: 45 },
  { eventId: '12346', total: 78 },
]

let reducedArray = array.reduce((acc, obj) => {
  console.log("Element " + JSON.stringify(obj));
  let accObj = acc.find(accObj => accObj.eventId === obj.eventId);
  if(accObj != undefined) {
    console.log("Found element: " + JSON.stringify(accObj) + ".")
    console.log("Pushing "+ obj.total +" to total.")
    accObj.total.push(obj.total);
  } else {
    accObj = { eventId: obj.eventId, total: [obj.total] };
    console.log("Pushing new element to list: " + JSON.stringify(accObj));
    acc.push(accObj);
  }
  return acc;
}, []);

console.log(JSON.stringify(reducedArray));

The trick is on the find method, I'm searching my own reducing list to see if it already has an element that shares eventId with the old array.

about 4 years ago · Santiago Gelvez Relatório

0

I'm no fan of .reduce so here is an easy solution without it

    const salesData = 
    [
      { eventId: '12345', total: 123 },
      { eventId: '12347', total: 45 },
      { eventId: '12349', total: 78 },
      { eventId: '12348', total: 123 },
      { eventId: '12346', total: 45 },
      { eventId: '12349', total: 78 },
      { eventId: '12347', total: 123 },
      { eventId: '12345', total: 45 },
      { eventId: '12348', total: 78 },
      { eventId: '12345', total: 123 },
      { eventId: '12347', total: 45 },
      { eventId: '12346', total: 78 },
      { eventId: '12349', total: 123 },
      { eventId: '12348', total: 45 },
      { eventId: '12346', total: 78 },
     ]
     
     let reduced = {}
     salesData.forEach(elem=> {
       if(reduced[elem.eventId] !== undefined){
         reduced[elem.eventId].push(elem.total)
       } else {
         reduced[elem.eventId] = [elem.total]
       }
     })
     reduced = Object.keys(reduced).map(elem => {
       return({eventId: elem, total:reduced[elem]})
     })
     console.log(reduced)

about 4 years ago · Santiago Gelvez 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