Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在时间戳数组中保持唯一的月份_Javascript_Reactjs_Ecmascript 6 - Fatal编程技术网

Javascript 如何在时间戳数组中保持唯一的月份

Javascript 如何在时间戳数组中保持唯一的月份,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我想创建一个数组,其中包含2015年8月、2015年9月等唯一月份。。为此,我定义了以下函数,该函数将带有时间戳的对象作为键: export function getUniqueMonths(exps) { //1. get all keys from expenditures const days = Object.keys(exps) //2. convert key strings to timestamps const daysInt = days.map((day)

我想创建一个数组,其中包含2015年8月、2015年9月等唯一月份。。为此,我定义了以下函数,该函数将带有时间戳的对象作为键:

export function getUniqueMonths(exps) {

  //1. get all keys from expenditures
  const days = Object.keys(exps)

  //2. convert key strings to timestamps
  const daysInt = days.map((day) => (new Date(parseInt(day))))

  //3. return only the "date portion" of the timestamp
  const datePortion = daysInt.map((day) => (new Date(day.toDateString()) ))

  //4. set each datePortion to 1st of month
  const firstOfMonth = datePortion.map((day) => new Date(day.getFullYear(), day.getMonth(), 1)  )

  //5. keep only unique firstOfMonths
  const uniqMonths = [...(new Set(firstOfMonth))]

  return uniqMonths
}
但是,此函数为我提供了如下数组:

[Sat Aug 01 2015 00:00:00 GMT+0300 (Eastern European Summer Time), Sat Aug 01 2015 00:00:00 GMT+0300 (Eastern European Summer Time), Tue Sep 01 2015 00:00:00 GMT+0300 (Eastern European Summer Time), Sat Aug 01 2015 00:00:00 GMT+0300 (Eastern European Summer Time), Sat Aug 01 2015 00:00:00 GMT+0300 (Eastern European Summer Time), ...]
我认为在步骤3中获取时间戳的日期部分,并将所有日期设置为第4步的月初就可以了。但我的数组中仍有重复项


我遗漏了什么?

我想你可能是过度设计了一些东西:比如

function getUniqueMonths(exps) {
  const uniqueMonths = new Set();
  Object.keys(exps).forEach((timestamp) => {
    const date = new Date(parseInt(timestamp));  // expected to be milliseconds since 1/1/1970
    uniqueMonths.add(`${date.getFullYear()}-${date.getMonth()}`);
  });
  return uniqueMonths;
}
按照JavaScript标准,应该以['2017-12'、'2018-0'、…]零基月份的形式为您提供一组独特的月份


如果您需要日期对象,那么这些对象再水化就很简单了。

我认为您可能过度设计了一些东西:比如

function getUniqueMonths(exps) {
  const uniqueMonths = new Set();
  Object.keys(exps).forEach((timestamp) => {
    const date = new Date(parseInt(timestamp));  // expected to be milliseconds since 1/1/1970
    uniqueMonths.add(`${date.getFullYear()}-${date.getMonth()}`);
  });
  return uniqueMonths;
}
按照JavaScript标准,应该以['2017-12'、'2018-0'、…]零基月份的形式为您提供一组独特的月份

如果您需要日期对象,则这些对象很容易再水化。

两个日期对象不是同一个对象,即使它们包含相同的时间戳

相反,请尝试:

//3. keep the year-month portion of the date
const yearMonths = daysInt.map(day => day.getFullYear()+"-"+day.getMonth());
然后你可以跳过4,从那里得到唯一的年-月数。例如,它们将作为2015年8月的2015-7返回。

两个日期对象不是相同的对象,即使它们包含相同的时间戳

相反,请尝试:

//3. keep the year-month portion of the date
const yearMonths = daysInt.map(day => day.getFullYear()+"-"+day.getMonth());

然后你可以跳过4,从那里得到唯一的年-月数。例如,这些将作为2015年8月的2015-7返回。

集合使用相等来确定两个元素是否相同。日期是对象,因此相等意味着它们必须是完全相同的日期对象,而不是引用同一时间点的两个不同日期对象。您可以在创建集合之前将日期对象转换为字符串,然后在.away之后再转换回来,只需在步骤4中添加.toString即可。谢谢!集合使用相等来确定两个元素是否相同。日期是对象,因此相等意味着它们必须是完全相同的日期对象,而不是引用同一时间点的两个不同日期对象。您可以在创建集合之前将日期对象转换为字符串,然后在.away之后再转换回来,只需在步骤4中添加.toString即可。谢谢!这给我带来了错误Uncaught TypeError:date.getFullYear不是一个函数-我想是因为Object.keys返回一个字符串数组。byDay:{'1440374400000':[{id:'caea6422-d208-4a94-a3a4-ea8b35ab8d39',createdAt:1480510965381,updatedAt:1480510965381,}]byDay是exps对象,object.KeyExps给我[1440374400000,1440720000000,1443398400000,…]我将替换uniqueMonths.add${date.getFullYear}-${date.getMonth};by uniqueMonths.adddate.toLocaleDateStringen-US,{year:'数字',month:'长'};这给我带来了错误Uncaught TypeError:date.getFullYear不是一个函数-我想是因为Object.keys返回一个字符串数组。byDay:{'1440374400000':[{id:'caea6422-d208-4a94-a3a4-ea8b35ab8d39',createdAt:1480510965381,updatedAt:1480510965381,}]byDay是exps对象,object.KeyExps给我[1440374400000,1440720000000,1443398400000,…]我将替换uniqueMonths.add${date.getFullYear}-${date.getMonth};by uniqueMonths.adddate.toLocaleDateStringen-US,{year:'numeric',month:'long};