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

114
Visualizações
Reduce array of arrays, concat data by dates to week data and sum the values

So I have an peculiar set of data:

data: [
  ['2022-01-17T16:29:24', 30], // these below would aggregate to 210
  ['2022-01-17T17:37:24', 30],
  ['2022-01-17T17:41:40', 30],
  ['2022-01-17T17:41:48', 30],
  ['2022-01-21T12:50:18', 30],
  ['2022-01-21T12:50:18', 30],
  ['2022-01-21T12:50:29', 30],        
  ['2022-01-12T12:50:18', 30], // these below would aggregate to 90
  ['2022-01-12T12:50:18', 30],
  ['2022-01-12T12:50:29', 30]
]

I would need to reduce this to sets of week data. I would convert dates to start of week to get the week data moment(day).startOf('isoWeek') and then concat the data on similar dates and reduce it to single entry.

Ending result would look like:

data: [
  ['2022-01-17T00:00:00', 210],
  ['2022-01-10T00:00:00', 90]
]

But I am looking for suggestions how to use .reduce() or any other more modern solutions for this that would be performant on large sets. Right now I have nested for loops for this which (aside from looking bad) probably perform quite poorly because it iterates multiple times over same array. One requirement is that moment should be used for date manipulation, please try to avoid Date obj's.

Right now I have something like this:

mergeDataToWeek(data: any[]) {
  let reducedArrays = [];
  for (let arr of data) {
    if (moment(arr[0]).isValid() && arr[1] > 0) {
      arr[0] = moment(arr[0]).startOf('isoWeek').toISOString(true);
      if (!reducedArrays.some(reduced => reduced[0] == (arr[0]))) {
        reducedArrays.push(arr);
      } else {
        let target = reducedArrays.find(reduced => reduced[0] == arr[0]);
        target[1] += arr[1];
      }
    }
  }
  console.log(reducedArrays);
  return reducedArrays;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I'm trying to solve it using reduce, but the result will be an object. Like this:

const moment = require("moment")
var x = [
    ['2022-01-17T16:29:24', 30], 
    ['2022-01-17T17:37:24', 30],
    ['2022-01-17T17:41:40', 30],
    ['2022-01-17T17:41:48', 30],
    ['2022-01-21T12:50:18', 30],
    ['2022-01-21T12:50:18', 30],
    ['2022-01-21T12:50:29', 30],        
    ['2022-01-12T12:50:18', 30], 
    ['2022-01-12T12:50:18', 30],
    ['2022-01-12T12:50:29', 30]
  ]

// solution
var y = x.reduce((a,b) => {
    const key = moment(b[0]).startOf("isoWeek").format()
    a[key] = a[key]? a[key]+b[1]: b[1]
    return a
}, {})

console.log(y)

Output:

{ 
  "2022-01-17T00:00:00+07:00": 210,
  "2022-01-10T00:00:00+07:00": 90 
}

But if you want it as array as yours, we can convert it by:

var z = Object.keys(y).map(i => [i,y[i]])

// OR THIS -- Thanks to @ghybs
var z = Object.entries(y)

console.log(z)

Output:

[
  [ '2022-01-17T00:00:00+07:00', 210 ],
  [ '2022-01-10T00:00:00+07:00', 90 ]
]

Although it's not the best solution, but I think it will much more readable than nested loop.

about 4 years ago · Juan Pablo Isaza Relatório

0


Run The Code

let x = [
  ['2022-01-17T16:29:24', 30],
  ['2022-01-17T17:37:24', 30],
  ['2022-01-17T17:41:40', 30],
  ['2022-01-17T17:41:48', 30],
  ['2022-01-21T12:50:18', 30],
  ['2022-01-21T12:50:18', 30],
  ['2022-01-21T12:50:29', 30],
  ['2022-01-12T12:50:18', 30],
  ['2022-01-12T12:50:18', 30],
  ['2022-01-12T12:50:29', 30]
]

// solution
const groupedTotal = x.reduce((a, b) => {
  const key = moment(b[0]).startOf("isoWeek").format()
  a[key] = a[key] ? a[key] + b[1] : b[1]
  return a
}, {});

const totals = Object.entries(groupedTotal);

console.log("Grouped Total:", groupedTotal, "\n Totals:", totals);
<script src="https://unpkg.com/moment@2.29.1/moment.js"></script>

Note:- Code from @Rizal Ardhi Rahmadani...

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