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

313
Visualizações
React Native moment.js find sum of elements which fall in a date range otherwise return 0

Im working with react native chart kit, I want to be able to display a graph of spending in my react native app. I'm using moment.js and moment-range

calendar.forEach((range) => {
    const label = range.start.format('DD MMM')
    loaded.labels.push(label);

    const filtered = spendingData
        .filter(item => range.contains(moment(item.timestamp)))
        .reduce((a,b) => (a.spent || 0) + (b.spent || 0), 0);
    
        loaded.data.push(filtered);
});

setGraph({...graph, labels: loaded.labels, data: loaded.data});

The filter by its self returns the correct items, each time one or more objects fall within the range it should be then passed to the reduce function to add them.

Objects returned without reduce (dates with nothing under means that there where no spendings that day) this works fine:


26 Dec
---------
02 Jan
---------
09 Jan
---------
16 Jan

Object {
  "spent": 3.8,
  "timestamp": 1642713860026,
}

Object {
  "spent": 12,
  "timestamp": 1642714020254,
}

---------
23 Jan
---------
30 Jan

Object {
  "spent": 2.4,
  "timestamp": 1643582338356,
}

Object {
  "spent": 5.5,
  "timestamp": 1643582748421,
}

Data returned after reducing the item.spent values, this is where the problem lies:

0

0

0

12

0

5.5
about 4 years ago · Juan Pablo Isaza
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