Home > Enterprise >  Split Array of Objects using a key
Split Array of Objects using a key

Time:10-10

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"
}]

CodePudding user response:

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

CodePudding user response:

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)

  • Related