0

Hello I have a array like this.

const array = [
{date: "2022-02-22 18:00:00"}
{date: "2022-02-22 17:00:00"}
{date: "2022-02-22 18:00:00"}
{date: "2022-02-23 01:00:00"}
{date: "2022-02-23 10:00:00"}
{date: "2022-02-24 18:00:00"}
]

Here I have to keep only one object for each day. Here It should be like that-

[
{date: "2022-02-22 18:00:00"}
{date: "2022-02-23 01:00:00"}
{date: "2022-02-24 18:00:00"}
]

Here you can see 22, 23, 24 date object. Not same date twice. Can anyone help me. I can also use moments js. You I need to use momentjs then please tell me.

2
  • If there are multiple objects with the same date but different time, which one should be taken? Commented Feb 22, 2022 at 16:46
  • This first one. Commented Feb 22, 2022 at 16:47

2 Answers 2

2

There are many ways to accomplish that: This is one

const data = [
  {date: "2022-02-22 18:00:00"},
  {date: "2022-02-22 17:00:00"},
  {date: "2022-02-22 18:00:00"},
  {date: "2022-02-23 01:00:00"},
  {date: "2022-02-23 10:00:00"},
  {date: "2022-02-24 18:00:00"}
];
                                      // simplified explanation:
let reminder = [];
let result = data.filter( item =>  {  // 1st iteration:    | 2nd iteration:    | 3rd iteration:    | 4th iteration:    | 5th iteration:
  let date = item.date.split(' ')[0]; // 2022-02-22        | 2022-02-22        | 2022-02-22        | 2022-02-23        | 2022-02-24 
  if(reminder.includes(date)) {       // does not include  | reminder has this | reminder has this | does not include  | does not include
    return false;                     // will not get here | go to next item   | go to next item   | will not get here | will not get here
  }
  reminder.push(date);                // push to reminder  | (will never reach | (will never reach | push to reminder  | push to reminder 
  return true;                        // add to result     |  those lines)     |  those lines)     | add to result     | add to result
});

console.log('result', result)

Read more about the used functions: Array.filter(), String.split(), Array.includes() and Array.push()

Update, as discussed in the comments

If you are dealing with a big amount of data you may better use new Set() with Set.has() and Set.add() see the next example using new Set()

const data = [
  {date: "2022-02-22 18:00:00"},
  {date: "2022-02-22 17:00:00"},
  {date: "2022-02-22 18:00:00"},
  {date: "2022-02-23 01:00:00"},
  {date: "2022-02-23 10:00:00"},
  {date: "2022-02-24 18:00:00"}
];
                                      // simplified explanation:
let reminder = new Set();
let result = data.filter( item =>  {  // 1st iteration:    | 2nd iteration:    | 3rd iteration:    | 4th iteration:    | 5th iteration:
  let date = item.date.split(' ')[0]; // 2022-02-22        | 2022-02-22        | 2022-02-22        | 2022-02-23        | 2022-02-24 
  if(reminder.has(date)) {            // does not have     | reminder has this | reminder has this | does not have     | does not have
    return false;                     // will not get here | go to 3rd         | go to 4th         | will not get here | will not get here
  }
  reminder.add(date);                 // push to reminder  | (will never reach | (will never reach | push to reminder  | push to reminder 
  return true;                        // add to result     |  those lines)     |  those lines)     | add to result     | add to result
                                      // go to 2nd         |                   |                   | go to 5th         | finished
});

console.log('result', result);

Sign up to request clarification or add additional context in comments.

5 Comments

If reminder was a Set, you'd have an O(n) filter.
@danh interesting point! from stackoverflow.com/a/39010462/2008111 it depends on how much data will be processed and which operations are used. I guess not much of a game changer at the moment
The operation you'd be using is has, which wasn't tested in the answer you linked. Another answer in the same post includes a result for has: (stackoverflow.com/a/46190569/294949), though who knows how good the methodology was on either.
I make this more as a style point than about performance. As a personal pref, if there's a way to write an O(n) loop instead of an O(n^2) loop, I do it (even if it's slower at small scale). It says to a future reader, "I was thinking about this". On SO, I sometimes use a less efficient thing if it makes a point more clearly to a beginner OP, and maybe add an aside about speed. I think your answer is legit on that basis.
thanks @danh for your explanation. It's much appreciated! Answer has been updated using Set()
-1

The following should be what you want. Please let me know if this works for you.

const array = [
    {date: "2022-02-22 18:00:00"},
    {date: "2022-02-22 17:00:00"},
    {date: "2022-02-22 18:00:00"},
    {date: "2022-02-23 01:00:00"},
    {date: "2022-02-23 10:00:00"},
    {date: "2022-02-24 18:00:00"}
]

// Slice first 10 characters of array and check for duplicates. Keep first element found.
const unique = array.reduce((acc, curr) => {
    if (acc.length === 0) {
        acc.push(curr)
    } else {
        const found = acc.find(e => e.date.slice(0, 10) === curr.date.slice(0, 10))
        if (!found) {
            acc.push(curr)
        }
    }
    return acc
}, [])

console.log(unique)

5 Comments

This converts the strings to date objects, which was not asked by OP...
I have made an amendment as OP requested.
Hello, Why only 10 items?
Why not 20, 30, 40 items?\
This will work for any amount of items. Comment was poor. It was supposed to be slice first 10 characters not elements.

Your Answer

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