代码之家  ›  专栏  ›  技术社区  ›  user3142695

如何从对象数组中获取唯一的日期值

  •  0
  • user3142695  · 技术社区  · 5 年前

    我要把所有的 格式中的多个日期值 DD.MM. . 在这个示例数据中,12月24日有两个值:

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

    所以结果应该是

    const result = [
        '24.12.',
        '25.12.',
        '26.12.'
    ]
    

    因此,首先,我将映射我的数据并仅对日期拆分值:

    const dates = data.map(d => d.date.toString().split('T')[0])
    

    但是,如何获取唯一值并更改输出格式?


    更新

    我想到了这个,但看起来很复杂。。。

    data.map(d => {
      const dateSplit = d.date.toString().split('T')[0].split('-')
      return dateSplit[2] + '.' + dateSplit[1] + '.'
    })
    .filter((value, index, self) {
      return self.indexOf(value) === index
    })
    
    5 回复  |  直到 5 年前
        1
  •  1
  •   Ori Drori    5 年前

    似乎ISODate返回了一个标准的JS日期对象。你可以用 Date.getDate() 为了得到今天,而且 Date.getMonth() 要获取月份(基于0,因此需要添加1):

    const data = [
      { date: new Date('2019-12-24T03:24:00Z') },
      { date: new Date('2019-12-24T04:56:00Z') },
      { date: new Date('2019-12-25T02:34:00Z') },
      { date: new Date('2019-12-26T01:23:00Z') }
    ]
    
    const result = [...new Set(data.map(({ date: d }) => 
      `${d.getDate()}.${d.getMonth() + 1}.`
    ))]
    
    console.log(result)

    上一个答案:

    使用正则表达式匹配月和日,并使用析构化将它们分配给const。使用模板文本组装字符串。通过将值赋给一个集合,然后扩展回一个数组来删除重复项。

    注意 :因为我没有访问ISODate的权限,所以我删除了它。我离开了 .toString() 虽然在本例中不需要它,但是在与ISODate一起使用时需要它。

    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 pattern = /-([0-9]{2})-([0-9]{2})T/
    
    const result = [...new Set(data.map(d => {
      const [, mon, day] = d.date.toString().match(pattern)
      
      return `${day}.${mon}.`;
    }))]
    
    console.log(result)
        2
  •  0
  •   Yousername    5 年前

    使用 .filter() 只过滤第一个值的值。

    //temporary function
    const ISODate = (d) => d;
    
    const data = [{
        date: ISODate("2019-12-24T03:24:00Z")
      },
      {
        date: ISODate("2019-12-24T04:56:00Z")
      },
      {
        date: ISODate("2019-12-25T02:34:00Z")
      },
      {
        date: ISODate("2019-12-26T01:23:00Z")
      }
    ]
    
    const dates = data.map(d => d.date.toString().split('T')[0].split("-").slice(1, 3).reverse().join(".") + ".")
    
    console.log(dates.filter((v, i, a) => a.indexOf(v) === i));
        3
  •  0
  •   mwilson    5 年前

    通过使用 Array.reduce . 注意我转换了 ISODate 成为 Date 既然我没有那门课,但应该是同一个概念。

    const data = [
        { date: new Date("2019-12-24T03:24:00Z") },
        { date: new Date("2019-12-24T04:56:00Z") },
        { date: new Date("2019-12-25T02:34:00Z") },
        { date: new Date("2019-12-26T01:23:00Z") }
    ];
    const result = data.reduce( (acc, curr) => {
      if (acc.length > 0) {
        const hasDate = acc.find(d => d.date.getMonth() === curr.date.getMonth() && d.date.getDate() === curr.date.getDate());
        if (!hasDate) { acc.push(curr); }
      } else {
        acc.push(curr);
      }
      return acc;
    }, []);
    
    console.log(result);
        4
  •  -1
  •   Ken Keenan    5 年前

    我会用 uniq Underscore.js 图书馆:

    const data = [
        { date: ISODate("2019-12-24T03:24:00Z") },
        { date: ISODate("2019-12-24T04:56:00Z") },
        { date: ISODate("2019-12-25T02:34:00Z") },
        { date: ISODate("2019-12-26T01:23:00Z") }
    ];
    
    let dates = _.uniq(data.map(d => d.date.toString().split('T')[0]));
    
        5
  •  -1
  •   shuk    5 年前

    一个相当不错的方法是:

    const array = [1, 2, 6, 5,5, 5, 3, 7, 8];
    
    const uniqueKeys = array.reduce((hashMap, value) => {
      if (!hashMap[value]) {
        hashMap[value] = true;
      }
      return hashMap;
    }, {});
    
    const uniqueValues = Object.keys(uniqueKeys);
    
    console.log(uniqueValues);
    

    它很好,因为它迭代数组一次,而不是像 .filter() 例子

    const array = [1, 2, 6, 5,5, 5, 3, 7, 8];
    
    const uniqueKeys = array.reduce((hashMap, value) => {
      if (!hashMap[value]) {
        hashMap[value] = true;
      }
      return hashMap;
    }, {});
    
    const uniqueValues = Object.keys(uniqueKeys);
    
    console.log(uniqueValues);