Filter array and add a property

0

I have an array listadoAvances where the progress of several projects per period is found.

The current period: 06/21/2018 or in seconds 1529539200, has information on the 3 projects: project 20, 21 and 22.

In the other period: 06/14/2018, exactly one week before, there is only information about two projects on 20 and 21. Because 22 did not yet exist.

  listadoAvances = [
    // periodo vigente 06/21/2018
    {
      proyecto: 20, 
      timestamp: 1529539200, // segundos
      vigente: true,
      avance: 90
    },
    {
      proyecto: 21, 
      timestamp: 1529539200,
      vigente: true,
      avance: 70
    },
    {
      proyecto: 22,
      timestamp: 1529539200,
      vigente: true,
      avance: 100
    },
    // periodo 06/14/2018
    {
      proyecto: 20, 
      timestamp: 1528934400, // 1 semana antes de vigente (1529539200 - 604800)
      vigente: false,
      avance: 80
    },
    {
      proyecto: 21, 
      timestamp: 1528934400,
      vigente: false,
      avance: 50
    }  
  ]

What I need is to get a list of only current projects, but to include a new property incremento , which is equal to the difference between the current progress and the previous week of the corresponding project.

For example:

  listadoAvancesVigente = [
    {
      proyecto: 20, 
      timestamp: 1529539200,
      vigente: true,
      avance: 90,
      incremento: 10 // diferencia entre avance vigente y el de la semana anterior
    },
    {
      proyecto: 21, 
      timestamp: 1529539200,
      vigente: true,
      avance: 70,
      incremento: 20
    },
    {
      proyecto: 22,
      timestamp: 1529539200,
      vigente: true,
      avance: 100,
      incremento: 0 // no existia en la semana anterior, de forma que no hubo incremento.
    }
  ]

Filtering current projects seems simple,

  listadoAvances.filter(p => {
    return p.vigente === true
  })

However, I'm not clear on how I could add the incremento property according to my requirement.

I appreciate your cooperation

    
asked by Javier Cárdenas 10.11.2018 в 20:24
source

1 answer

0

try this code:

listadoAvances = [
  // periodo vigente 06/21/2018
  {
    proyecto: 20, 
    timestamp: 1529539200, // segundos
    vigente: true,
    avance: 90
  },
  {
    proyecto: 21, 
    timestamp: 1529539200,
    vigente: true,
    avance: 70
  },
  {
    proyecto: 22,
    timestamp: 1529539200,
    vigente: true,
    avance: 100
  },
  // periodo 06/14/2018
  {
    proyecto: 20, 
    timestamp: 1528934400, // 1 semana antes de vigente (1529539200 - 604800)
    vigente: false,
    avance: 80
  },
  {
    proyecto: 21, 
    timestamp: 1528934400,
    vigente: false,
    avance: 50
  }  
]
/* listadoAvancesVigente = [
  {
    proyecto: 20, 
    timestamp: 1529539200,
    vigente: true,
    avance: 90,
    incremento: 10 // diferencia entre avance vigente y el de la semana anterior
  },
  {
    proyecto: 21, 
    timestamp: 1529539200,
    vigente: true,
    avance: 70,
    incremento: 20
  },
  {
    proyecto: 22,
    timestamp: 1529539200,
    vigente: true,
    avance: 100,
    incremento: 0 // no existia en la semana anterior, de forma que no hubo incremento.
  }
] */
const proyectosAnteriores = listadoAvances.filter(p => p.timestamp === 1528934400)
const proyectosVigentes = listadoAvances.filter(p => p.vigente === true)

proyectosVigentes.forEach((p,i) => {
  const pa = proyectosAnteriores[i]
  if(pa === undefined){
    p.incremento = 0
  }else{
    p.incremento = p.avance - pa.avance
  }
  console.log(p)
});
    
answered by 12.11.2018 в 15:52