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

108
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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