4

 如果今天是 2018-11-28,我有一个这样的对象数组:

  const dataset = [
  {
    "timestamp": "2018-11-28T16:38:07.610Z",
    "value": 751.998557581834
  },
  {
    "timestamp": "2018-11-27T16:38:07.610Z",
    "value": 644.9987628195244
  },
  {
    "timestamp": "2018-11-26T16:38:07.610Z",
    "value": 766.9985288101943
  },
  {
    "timestamp": "2018-11-25T16:38:07.610Z",
    "value": 953.9981701237627
  },
  {
    "timestamp": "2018-11-24T16:38:07.610Z",
    "value": 912.9982487662423
  },
  {
    "timestamp": "2018-11-23T16:38:07.610Z",
    "value": 402
  },
  {
    "timestamp": "2018-11-22T16:38:07.610Z",
    "value": 914.9982449300243
  },
  {
    "timestamp": "2018-11-21T16:38:07.610Z",
    "value": 769.9985230558668
  },
  {
    "timestamp": "2018-11-20T16:38:07.610Z",
    "value": 772.9985173015398
  },
  {
    "timestamp": "2018-11-19T16:38:07.610Z",
    "value": 176
  },
  {
    "timestamp": "2018-11-18T16:38:07.610Z",
    "value": 978.9981221710306
  },
  {
    "timestamp": "2018-11-17T16:38:07.611Z",
    "value": 342
  },
  {
    "timestamp": "2018-11-16T16:38:07.611Z",
    "value": 498.9990428634777
  },
  {
    "timestamp": "2018-11-15T16:38:07.611Z",
    "value": 326
  },
  {
    "timestamp": "2018-11-14T16:38:07.612Z",
    "value": 649.9987532289786
  },
  {
    "timestamp": "2018-11-13T16:38:07.612Z",
    "value": 70
  },
  {
    "timestamp": "2018-11-12T16:38:07.612Z",
    "value": 349
  },
  {
    "timestamp": "2018-11-11T16:38:07.612Z",
    "value": 191
  },
  {
    "timestamp": "2018-11-10T16:38:07.612Z",
    "value": 154
  },
  {
    "timestamp": "2018-11-09T16:38:07.613Z",
    "value": 109
  },
  {
    "timestamp": "2018-11-08T16:38:07.613Z",
    "value": 237
  },
  {
    "timestamp": "2018-11-07T16:38:07.613Z",
    "value": 398
  },
  {
    "timestamp": "2018-11-06T16:38:07.613Z",
    "value": 606.9988357076774
  },
  {
    "timestamp": "2018-11-05T16:38:07.614Z",
    "value": 131
  },
  {
    "timestamp": "2018-11-04T16:38:07.614Z",
    "value": 397
  },
  {
    "timestamp": "2018-11-03T16:38:07.614Z",
    "value": 583.9988798241893
  },
  {
    "timestamp": "2018-11-02T16:38:07.614Z",
    "value": 362
  },
  {
    "timestamp": "2018-11-01T16:38:07.614Z",
    "value": 686.998682258936
  },
  {
    "timestamp": "2018-10-31T16:38:07.614Z",
    "value": 131
  },
  {
    "timestamp": "2018-10-30T16:38:07.614Z",
    "value": 212
  }
]

使用以下代码创建对象:

  import { DateTime } from 'luxon'

  const timestamp = startDate.minus({ days: i }).toJSDate()
  return { timestamp: timestamp, value: randomValue }

我想要包含本月第一天的对象,所以在这个例子中,我想要:

  {
    "timestamp": "2018-11-01T16:38:07.614Z",
    "value": 686.998682258936
  }

这是我尝试过的:

const date = new Date()
const firstDayOfThisMonth = new Date(date.getFullYear(), date.getMonth(), 1)
const firstDayOfThisMonthSub = firstDayOfThisMonth.toString().substring(0, 15)
const bo = dataset.map((d, i) => {
  const sub = d.toString().substring(0, 15)
  if (sub === firstDayOfThisMonthSub) return d
})

它不起作用(我得到了一个数组undefined),我希望有一种更聪明的方法来做到这一点。我可以使用 JavascriptDate对象或Luxon库。

谢谢

4

3 回答 3

3

与 luxon :

const firstDayOfThisMonth = DateTime.local().startOf('month')
const firstDayRecord = dataset.find(record => {
 return DateTime.fromISO(record.timestamp).hasSame(firstDayOfThisMonth, 'day')
})

这应该可以解决问题

于 2018-11-28T18:35:30.693 回答
1

您有 UTC 时间戳,但我不清楚您是想要当地的一个月的第一天还是 UTC 的一个月的第一天。如果您需要 UTC,则可以将每月的第一天作为 ISO 8601 日期简化为:

let date = new Date().toISOString().slice(0,8) + '01';

请注意,UTC 月份将与本地月份不同,因为主机时区在月初或月底偏移,具体取决于格林威治东部或西部。然后您可以使用过滤器来获取匹配的元素,例如

var data = 
  [{"timestamp": "2018-11-02T16:38:07.614Z","value": 362},
   {"timestamp": "2018-11-01T16:38:07.614Z","value": 686.998},
   {"timestamp": "2018-10-31T16:38:07.614Z","value": 131},
   {"timestamp": "2018-10-30T16:38:07.614Z","value": 212}];
   
var s = new Date().toISOString().slice(0,8) + '01';
var d = data.filter(o => o.timestamp.slice(0,10) == s);
console.log(d);

但是,如果您想找到当地当月第一天的时间戳,您应该将时间戳转换为日期并与当地当月第一天的开始和结束进行比较,例如

var data = 
  [{"timestamp": "2018-11-02T16:38:07.614Z","value": 362},
   {"timestamp": "2018-11-01T16:38:07.614Z","value": 686.998},
   {"timestamp": "2018-10-31T16:38:07.614Z","value": 131},
   {"timestamp": "2018-10-30T16:38:07.614Z","value": 212}];
    
var d = new Date();
d.setDate(1);
let firstOfMonthStart = d.setHours(0,0,0,0);
let firstOfMonthEnd   = d.setHours(23,59,59,999);
let t = data.filter(o => {
  let d = new Date(o.timestamp);
  return d >= firstOfMonthStart && d <= firstOfMonthEnd;
});
console.log(t);

请注意,firstOfMonthStartfirstOfMonthEnd将是时间值,而不是日期,但比较有效,因为<并将>值强制为数字,因此比较就像它们是日期一样工作。

对于本地时区为 +10:00 的人,2018 年 11 月返回的数组为:

[{timestamp: "2018-10-31T16:38:07.614Z", value: 131}]

因为他们当地的月初是 2018-10-31T14:00:00Z。

于 2018-11-28T20:52:19.693 回答
0

map()不是正确的工具。它将为原始数组中的每个项目提供一个值,即使它是undefined. 要获取数组的子集,请使用filter().

因为这些都是不错的 ISO 8601 日期字符串。您可以构建一个日期字符串,如 '2018-11-01' 并根据 dat 是否以它开头进行过滤:

let a = [  {"timestamp": "2018-11-28T16:38:07.610Z","value": 751.998557581834},{"timestamp": "2018-11-27T16:38:07.610Z","value": 644.9987628195244},{"timestamp": "2018-11-26T16:38:07.610Z","value": 766.9985288101943},{"timestamp": "2018-11-25T16:38:07.610Z","value": 953.9981701237627},{"timestamp": "2018-11-24T16:38:07.610Z","value": 912.9982487662423},{"timestamp": "2018-11-23T16:38:07.610Z","value": 402},{"timestamp": "2018-11-22T16:38:07.610Z","value": 914.9982449300243},{"timestamp": "2018-11-21T16:38:07.610Z","value": 769.9985230558668},{"timestamp": "2018-11-20T16:38:07.610Z","value": 772.9985173015398},{"timestamp": "2018-11-19T16:38:07.610Z","value": 176},{"timestamp": "2018-11-18T16:38:07.610Z","value": 978.9981221710306},{"timestamp": "2018-11-17T16:38:07.611Z","value": 342},{"timestamp": "2018-11-16T16:38:07.611Z","value": 498.9990428634777},{"timestamp": "2018-11-15T16:38:07.611Z","value": 326},{"timestamp": "2018-11-14T16:38:07.612Z","value": 649.9987532289786},{"timestamp": "2018-11-13T16:38:07.612Z","value": 70},{"timestamp": "2018-11-12T16:38:07.612Z","value": 349},{"timestamp": "2018-11-11T16:38:07.612Z","value": 191},{"timestamp": "2018-11-10T16:38:07.612Z","value": 154},{"timestamp": "2018-11-09T16:38:07.613Z","value": 109},{"timestamp": "2018-11-08T16:38:07.613Z","value": 237},{"timestamp": "2018-11-07T16:38:07.613Z","value": 398},{"timestamp": "2018-11-06T16:38:07.613Z","value": 606.9988357076774},{"timestamp": "2018-11-05T16:38:07.614Z","value": 131},{"timestamp": "2018-11-04T16:38:07.614Z","value": 397},{"timestamp": "2018-11-03T16:38:07.614Z","value": 583.9988798241893},{"timestamp": "2018-11-02T16:38:07.614Z","value": 362},{"timestamp": "2018-11-01T16:38:07.614Z","value": 686.998682258936},{"timestamp": "2018-10-31T16:38:07.614Z","value": 131},{"timestamp": "2018-10-30T16:38:07.614Z","value": 212}]

let now =  new Date()
 // start of month is always 01
 // month is zero indexed
let pattern = `${now.getUTCFullYear()}-${now.getUTCMonth()+1}-01`   
let filtered = a.filter(item => item.timestamp.startsWith(pattern))
console.log(filtered)

于 2018-11-28T18:28:37.390 回答