skip to Main Content

How to find second unique date using JS, e.g. output should return 2019-12-25T02:34:00Z

const data = [
  { date: '2019-12-24T03:24:00Z' },
  { date: '2019-12-24T04:56:00Z' },
  { date: '2019-12-25T02:34:00Z' },
  { date: '2019-12-26T01:23:00Z' }
]

2

Answers


  1. Collect dates with counts and find the first date with count === 1 (unique):

    const data = [
      { date: '2019-12-24T03:24:00Z' },
      { date: '2019-12-24T04:56:00Z' },
      { date: '2019-12-25T02:34:00Z' },
      { date: '2019-12-26T01:23:00Z' }
    ];
    
    const result = Object.values(data.reduce((r, {date: item}) => {
      const date = item.slice(0, 10);
      date in r ? r[date].count++ : r[date] = {count:1, item};
      return r;
    }, {})).find(({count}) => count === 1)?.item || null;
    
    console.log(result);
    Login or Signup to reply.
  2. You could reduce (right) all the dates and build a lookup may by unique date-only. Reducing right will keep the first value as it appears in the original array, but will reverse the output, so you should reverse at the very end.

    const data = [
      { date: '2019-12-24T03:24:00Z' },
      { date: '2019-12-24T04:56:00Z' },
      { date: '2019-12-25T02:34:00Z' },
      { date: '2019-12-26T01:23:00Z' }
    ];
    
    const uniqueByDate = (data) => data.reduceRight((lookup, { date }) =>
      lookup.set(new Date(date).toLocaleDateString(), date), new Map);
    
    const uniqueTimestampsByDate = [...uniqueByDate(data).entries()].reverse();
    
    const getNthByUniqueDate = (index) =>
      index > -1 && index < uniqueTimestampsByDate.length
        ? uniqueTimestampsByDate[index][1]
        : null;
    
    for (let i = 0; i < uniqueTimestampsByDate.length; i++) {
      console.log(`Unique at index: ${i} = ${getNthByUniqueDate(i)}`);
    }

    Output:

    Unique at index: 0 = 2019-12-24T03:24:00Z
    Unique at index: 1 = 2019-12-25T02:34:00Z
    Unique at index: 2 = 2019-12-26T01:23:00Z
    

    Here is a version without reversing. It requires a convenience function to only update the lookup map, if the key does not already exist.

    const data = [
      { date: '2019-12-24T03:24:00Z' },
      { date: '2019-12-24T04:56:00Z' },
      { date: '2019-12-25T02:34:00Z' },
      { date: '2019-12-26T01:23:00Z' }
    ];
    
    const setIfNotPresent = (map, key, value) =>
      !map.has(key) ? map.set(key, value) : map;
    
    const uniqueByDate = (data) => data.reduce((lookup, { date }) =>
      setIfNotPresent(lookup, new Date(date).toLocaleDateString(), date), new Map);
    
    const uniqueTimestampsByDate = [...uniqueByDate(data).entries()];
    
    const getNthByUniqueDate = (index) =>
      index > -1 && index < uniqueTimestampsByDate.length
        ? uniqueTimestampsByDate[index][1]
        : null;
    
    for (let i = 0; i < uniqueTimestampsByDate.length; i++) {
      console.log(`Unique at index: ${i} = ${getNthByUniqueDate(i)}`);
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search