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

216
Views
Group objects in array based on sequence

I have an array of objects that are a log of actions taken by users. This is an example of what the array looks like:

   [
    {
        "timestamp": "2022-07-14T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-13T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-12T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-11T14:53:32.925Z",
        "action": "Action 2",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-10T14:53:32.925Z",
        "action": "Action 2",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-09T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-08T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-07T14:53:32.925Z",
        "action": "Action 3",
        "user": "User B"
    }
]

The result I am trying to achieve is that the array should be:

[
    {
        "timestamp": "2022-07-14T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-11T14:53:32.925Z",
        "action": "Action 2",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-09T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-07T14:53:32.925Z",
        "action": "Action 3",
        "user": "User B"
    }
]

The array is in order of time.

I don't want to group all duplicates, for example all "action 1" by "User A", only if they occurred one after the other, save the most recent

Any suggestions would be greatly appreciated, TIA!

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

We can use Array.reduce() to add each item to our output array (acc) if it meets any of the criteria:

  • Action has changed.
  • User has changed.
  • Timestamp is greater than the last item.

This should give the required result.

const input = [ { "timestamp": "2022-07-14T14:53:32.925Z", "action": "Action 1", "user": "User A" }, { "timestamp": "2022-07-13T14:53:32.925Z", "action": "Action 1", "user": "User A" }, { "timestamp": "2022-07-12T14:53:32.925Z", "action": "Action 1", "user": "User A" }, { "timestamp": "2022-07-11T14:53:32.925Z", "action": "Action 2", "user": "User A" }, { "timestamp": "2022-07-10T14:53:32.925Z", "action": "Action 2", "user": "User A" }, { "timestamp": "2022-07-09T14:53:32.925Z", "action": "Action 1", "user": "User A" }, { "timestamp": "2022-07-08T14:53:32.925Z", "action": "Action 1", "user": "User A" }, { "timestamp": "2022-07-07T14:53:32.925Z", "action": "Action 3", "user": "User B" } ];

const result = (input.reduce((acc, { timestamp, action, user }) => {
    const { action: lastAction, user: lastUser, timestamp: lastTimestamp } = acc[acc.length - 1] || {};
    if (lastAction !== action || lastUser !== user || lastTimestamp < timestamp) {
        acc.push({ timestamp, action, user });
    }
    return acc;
}, []));

console.log('Result:', result);
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Santiago Trujillo Report

0

Assuming the input array is sorted from new to old. You can loop through them, check each action against the last action present in the result. If it has the same action/user combo, don't add it to the result. If it's not the same user/action, add it to the result. Repeat until you've iterated through all actions.

You can check action/user equality in different ways. One way to do this would be to use your own custom equality function.

const isEqual = (a, b) => a.action === b.action && a.user === b.user;
// you can then compare 2 actions using
isEqual(actionA, actionB)

function solution(actions) {
  const last = array => array[array.length - 1];
  const isEqual = (a, b) => a.action === b.action && a.user === b.user;
  
  const output = [];
  for (const inputAction of actions) {
    const outputAction = last(output) || {};

    // skip if the action/user combination is the same
    if (isEqual(inputAction, outputAction)) continue;
    
    output.push(inputAction);
  }
  return output;
}

console.log(solution([
    {
        "timestamp": "2022-07-14T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-13T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-12T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-11T14:53:32.925Z",
        "action": "Action 2",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-10T14:53:32.925Z",
        "action": "Action 2",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-09T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-08T14:53:32.925Z",
        "action": "Action 1",
        "user": "User A"
    },
    {
        "timestamp": "2022-07-07T14:53:32.925Z",
        "action": "Action 3",
        "user": "User B"
    }
]));

about 4 years ago · Santiago Trujillo Report

0

You could use javascrip function sort for you array:

const log = [
  { "timestamp": "2022-07-14T14:53:32.925Z", "action": "Action 1", "user": "User A" },
  { "timestamp": "2022-07-13T14:53:32.925Z", "action": "Action 1", "user": "User A" },
  { "timestamp": "2022-07-12T14:53:32.925Z", "action": "Action 1", "user": "User A" },
  { "timestamp": "2022-07-11T14:53:32.925Z", "action": "Action 2", "user": "User A" },
  { "timestamp": "2022-07-10T14:53:32.925Z", "action": "Action 2", "user": "User A" },
  { "timestamp": "2022-07-09T14:53:32.925Z", "action": "Action 1", "user": "User A" },
  { "timestamp": "2022-07-08T14:53:32.925Z", "action": "Action 1", "user": "User A" },
  { "timestamp": "2022-07-07T14:53:32.925Z", "action": "Action 3", "user": "User B" }
]

const uniqArray = (arr) => {
  return arr.filter((elem, pos, self) => {
    return self.findIndex((item) => item.timestamp == elem.timestamp) == pos
  })
}

const uniqueLog = uniqArray(log, (item) => item.timestamp)
const sortedLog = uniqueLog.sort((row) => new Date(row.timestamp).getTime())

console.log({ sortedLog })

about 4 years ago · Santiago Trujillo 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!