Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

109
Vistas
Split Array of Objects using a key

I have a data array like below which needs to be a structured by using Value property with Start and End Time

[{
        "Timestamp": "2021-09-30T21:38:46.7000122Z",
        "Value": "496",
    },
    {
        "Timestamp": "2021-10-01T01:08:47.4690093Z",
        "Value": "496",
    },
    {
        "Timestamp": "2021-10-02T15:38:02.5080108Z",
        "Value": "207",
    },
    {
        "Timestamp": "2021-10-02T16:30:32.3410034Z",
        "Value": "207",
    },
    {
        "Timestamp": "2021-10-02T21:45:32.7460021Z",
        "Value": "207",
    },
    {
        "Timestamp": "2021-10-02T22:38:02.5839996Z",
        "Value": "413",
    },
    {
        "Timestamp": "2021-10-02T23:30:33.3980102Z",
        "Value": "413",
    },
    {
        "Timestamp": "2021-10-03T00:23:02.7130126Z",
        "Value": "413",
    },
    {
        "Timestamp": "2021-10-03T11:47:47.8630065Z",
        "Value": "413",
    }]

Is there a way to compose the above array to into below format like group using Value and pick the the 1st object timestamp as Start Time and last object timestamp value as End Time like below

[{
"id": 496
"stTime": "2021-09-30T21:38:46.7000122Z"
"endTime": "2021-10-01T01:08:47.4690093Z"
},{
"id": 207
"stTime": "2021-10-02T15:38:02.5080108Z"
"endTime": "2021-10-02T21:45:32.7460021Z"
},{
"id": 413
"stTime": "2021-10-02T22:38:02.5839996Z"
"endTime": "2021-10-03T11:47:47.8630065Z"
}]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

const data = [
  {
    Timestamp: "2021-09-30T21:38:46.7000122Z",
    Value: "496",
  },
  {
    Timestamp: "2021-10-01T01:08:47.4690093Z",
    Value: "496",
  },
  {
    Timestamp: "2021-10-02T15:38:02.5080108Z",
    Value: "207",
  },
  {
    Timestamp: "2021-10-02T16:30:32.3410034Z",
    Value: "207",
  },
  {
    Timestamp: "2021-10-02T21:45:32.7460021Z",
    Value: "207",
  },
  {
    Timestamp: "2021-10-02T22:38:02.5839996Z",
    Value: "413",
  },
  {
    Timestamp: "2021-10-02T23:30:33.3980102Z",
    Value: "413",
  },
  {
    Timestamp: "2021-10-03T00:23:02.7130126Z",
    Value: "413",
  },
  {
    Timestamp: "2021-10-03T11:47:47.8630065Z",
    Value: "413",
  },
];


function processTimestamps(timestamps) {
  return timestamps.reduce((retval, { Timestamp, Value }) => {
    const currTime = new Date(Timestamp)
    const existing = retval.find(obj => obj.id === Value)
    if(existing){
      const startTime = new Date(existing.stTime)
      const endTime = new Date(existing.endTime)
      if (currTime < startTime){
        existing.startTime = Timestamp
      }
      if (currTime > endTime){
        existing.endTime = Timestamp
      }
    }else{
      retval.push({
        id: Value,
        stTime: Timestamp,
        endTime: Timestamp
      })
    }
    return retval
  }, []);
}

console.log(processTimestamps(data));
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Object.values() to extract teh values result array of Array.prototype.reduce()ing the data

Code:

const data = [{Timestamp: '2021-09-30T21:38:46.7000122Z',Value: '496',},{Timestamp: '2021-10-01T01:08:47.4690093Z',Value: '496',},{Timestamp: '2021-10-02T15:38:02.5080108Z',Value: '207',},{Timestamp: '2021-10-02T16:30:32.3410034Z',Value: '207',},{Timestamp: '2021-10-02T21:45:32.7460021Z',Value: '207',},{Timestamp: '2021-10-02T22:38:02.5839996Z',Value: '413',},{Timestamp: '2021-10-02T23:30:33.3980102Z',Value: '413',},{Timestamp: '2021-10-03T00:23:02.7130126Z',Value: '413',},{Timestamp: '2021-10-03T11:47:47.8630065Z',Value: '413'}]

const result = Object
  .values(
    data.reduce((a, { Value, Timestamp }) => {
      a[Value] = a[Value] || {
        id: +Value,
        stTime: Timestamp,
      }
      a[Value].endTime = Timestamp
      return a
    }, {})
  )

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

const data = [{Timestamp: '2021-09-30T21:38:46.7000122Z',Value: '496',},{Timestamp: '2021-10-01T01:08:47.4690093Z',Value: '496',},{Timestamp: '2021-10-02T15:38:02.5080108Z',Value: '207',},{Timestamp: '2021-10-02T16:30:32.3410034Z',Value: '207',},{Timestamp: '2021-10-02T21:45:32.7460021Z',Value: '207',},{Timestamp: '2021-10-02T22:38:02.5839996Z',Value: '413',},{Timestamp: '2021-10-02T23:30:33.3980102Z',Value: '413',},{Timestamp: '2021-10-03T00:23:02.7130126Z',Value: '413',},{Timestamp: '2021-10-03T11:47:47.8630065Z',Value: '413'}]

var res = data.map(e=>e.Value).filter((e,i,a)=>a.indexOf(e)==i).map(e=>({id:e,times:time.filter(x=>x.Value==e).map(x=>x.Timestamp)}))
.map(e=>({t:e,s:new Date(e)})).sort((a,b)=>a.s-b.s > 1).map(e=>e.t)
.map(e=>({id:e.id,stTime:e.times.shift(),endTime:e.times.pop()}))
console.log(res);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda