2

I using array inside array like the example below the left element is user Id and the right element is a timestamp value

[[2222,2344556], [1111,2344546], [1111,2345556], [1111,2347556]]

I'm trying to do the following :

  1. filter users with more than 60 sec, to do so I used this code and it working well

    recived_msg_user.filter(([user, date]) => Math.abs(date - nowTimeStamp) < 60000 
    
  2. Now I'm trying to add a filter for showing uniqueness users with the recent timestamp

    [[2222,2344556], [1111,2347556]]
    

How to do so? there is an option to do it in the same filter?

2 Answers 2

1

Here is some low level bubbling with exclusion. Should be speedy enough.

function filter(array) {
    let filtered = [];
    let checkedIndexes = [];
    for (let i = 0; i < array.length; i++) {
        if (!(Math.abs(item[1] - nowTimeStamp) < 60000) ||
                                 checkedIndexes.includes(i)) continue;
        let index = i;
        let time = array[i][1];
        for (let j = i + 1; j < array.length; j++) {
            if (array[j][0] == array[i][0]) {
                checkedIndexes.push(j);
                if (array[j][1] > time) {
                    index = j;
                    time = array[j][1];
                }
            }
        }
        filtered.push(array[index]);
    }
    return filtered;
}
Sign up to request clarification or add additional context in comments.

Comments

0

In terms of being in the same filter, I'm not sure if there is an option to do that. However, on a separate line you can use the array method of sort then sort by the lowest timestamp. Here's the code

yourArray.sort((a, b) => a[1] - b[1]);

Should change the array to order everything by the lowest timestamp to the greatest timestamp. I hope this helps.

4 Comments

What do you mean by unique users, users with different Ids or users with special Ids or something else?
every user-id should show only one time in the result include his latest timestamp
Ah I see, so keep the user ids with the latest timestamp and remove the other same user ids
Yes, this is the second part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.