Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!