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.

caramba
  • 21,963
  • 19
  • 86
  • 127

2 Answers2

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);
caramba
  • 21,963
  • 19
  • 86
  • 127
  • If `reminder` was a Set, you'd have an O(n) filter. – danh Feb 22 '22 at 17:33
  • @danh interesting point! from https://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 – caramba Feb 22 '22 at 18:30
  • 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: (https://stackoverflow.com/a/46190569/294949), though who knows how good the methodology was on either. – danh Feb 22 '22 at 19:03
  • 1
    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. – danh Feb 22 '22 at 19:04
  • thanks @danh for your explanation. It's much appreciated! Answer has been updated using Set() – caramba Feb 23 '22 at 06:40
-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)
Aidan Donnelly
  • 369
  • 5
  • 19