分享
三行代码  ›  专栏  ›  技术社区  ›  Firmansyah

如何知道数组对象中有多少数据具有不同的日期或日期

  •  0
  • Firmansyah  · 技术社区  · 5 天前

    大家好!
    object 里面 array Unix Time 或者一个日期,我想计算有多少数据有不同的日期或日期,目标是取每天、每周、每月和每年的平均收入值。

    let MyData = [
      {
        date: 1592918643305,
        name: 'Alex'
      },
      {
        date: 1592921952307,
        name: 'Simon'
      },
      {
        date: 1592927378630,
        name: 'Fory'
      },
      {
        date: 1592987102694,
        name: 'John'
      },
      {
        date: 1592987102694,
        name: 'Erwin'
      },
      {
        date: 1593422810672,
        name: 'Muller'
      },
      {
        date: 1593424811785,
        name: 'Thomas'
      },
      {
        date: 1593478227245,
        name: 'Max'
      },
      {
        date: 1593478293013,
        name: 'Tom'
      }
    ];
    
    function convertToRegularDate(unix) {
      let regularDate = new Date(unix);
      let year = regularDate.getFullYear();
      let month = regularDate.getMonth();
      let date = regularDate.getDate();
      return 'Date: ' + date + '/' + month + '/' + year;
    }
    
    MyData.forEach(function (item) {
      console.log('Name: ' + item.name + ' ' +
      convertToRegularDate(item.date)
      );
    });
    3 回复  |  直到 5 天前
        1
  •  1
  •   holydragon    5 天前

    您可以通过推送从您编写的函数中处理的日期字符串来创建一个数组来存储唯一的日期。如果这些日期字符串是唯一的,那么您将如何比较它们。所以,在for-each中处理完所有数据之后,就可以得到数组的长度了。

    let MyData = [
      {
        date: 1592918643305,
        name: 'Alex'
      },
      {
        date: 1592921952307,
        name: 'Simon'
      },
      {
        date: 1592927378630,
        name: 'Fory'
      },
      {
        date: 1592987102694,
        name: 'John'
      },
      {
        date: 1592987102694,
        name: 'Erwin'
      },
      {
        date: 1593422810672,
        name: 'Muller'
      },
      {
        date: 1593424811785,
        name: 'Thomas'
      },
      {
        date: 1593478227245,
        name: 'Max'
      },
      {
        date: 1593478293013,
        name: 'Tom'
      }
    ];
    
    function convertToRegularDate(unix) {
      let regularDate = new Date(unix);
      let year = regularDate.getFullYear();
      let month = regularDate.getMonth();
      let date = regularDate.getDate();
      return 'Date: ' + date + '/' + month + '/' + year;
    }
    
    let uniqueDates = [];
    MyData.forEach(function (item) {
      let dateString = convertToRegularDate(item.date);
      if (!uniqueDates.includes(dateString)){
        uniqueDates.push(dateString);
      }
      console.log('Name: ' + item.name + ' ' + dateString);
    });
    console.log('Unique Date: ' + uniqueDates.length);
        2
  •  1
  •   Pain    5 天前

    let MyData = [
      {
        date: 1592918643305,
        name: 'Alex'
      },
      {
        date: 1592921952307,
        name: 'Simon'
      },
      {
        date: 1592927378630,
        name: 'Fory'
      },
      {
        date: 1592987102694,
        name: 'John'
      },
      {
        date: 1592987102694,
        name: 'Erwin'
      },
      {
        date: 1593422810672,
        name: 'Muller'
      },
      {
        date: 1593424811785,
        name: 'Thomas'
      },
      {
        date: 1593478227245,
        name: 'Max'
      },
      {
        date: 1593478293013,
        name: 'Tom'
      }
    ];
    
    
    function convertToRegularDate(unix) {
      let regularDate = new Date(unix);
      let year = regularDate.getFullYear();
      let month = regularDate.getMonth();
      let date = regularDate.getDate();
      return 'Date: ' + date + '/' + month + '/' + year;
    }
    
    MyData.forEach(function (item) {
      console.log('Name: ' + item.name + ' ' +
      convertToRegularDate(item.date)
      );
    }); 
    
    
    const getUniqDates = (data) => [...new Set(data.map(item => new Date(item.date).toLocaleDateString()))]
    const uniqDates = getUniqDates(MyData)
    console.log("You have " + uniqDates.length + " uniq dates  >> ", ...uniqDates )
        3
  •  0
  •   Menai Ala Eddine - Aladdin    5 天前

    Set .

    // Set is an object type that has unique values
    const set = new Set()
    
    // Iterate through your array
    for (let o of MyData) {
        set.add(o.date)
    }
    
    // convert to regular date
    set.forEach(unixDate => convertToRegularDate(unixDate))
    
    // print the count of data with different date
    console.log(set.size)