\xc2\xa0如果今天是 2018-11-28,我有一个像这样的对象数组:
\n\n const dataset = [\n {\n "timestamp": "2018-11-28T16:38:07.610Z",\n "value": 751.998557581834\n },\n {\n "timestamp": "2018-11-27T16:38:07.610Z",\n "value": 644.9987628195244\n },\n {\n "timestamp": "2018-11-26T16:38:07.610Z",\n "value": 766.9985288101943\n },\n {\n "timestamp": "2018-11-25T16:38:07.610Z",\n "value": 953.9981701237627\n },\n {\n "timestamp": "2018-11-24T16:38:07.610Z",\n "value": 912.9982487662423\n },\n {\n "timestamp": "2018-11-23T16:38:07.610Z",\n "value": 402\n },\n {\n "timestamp": "2018-11-22T16:38:07.610Z",\n "value": 914.9982449300243\n },\n {\n "timestamp": "2018-11-21T16:38:07.610Z",\n "value": 769.9985230558668\n },\n {\n "timestamp": "2018-11-20T16:38:07.610Z",\n "value": 772.9985173015398\n },\n {\n "timestamp": "2018-11-19T16:38:07.610Z",\n "value": 176\n },\n {\n "timestamp": "2018-11-18T16:38:07.610Z",\n "value": 978.9981221710306\n },\n {\n "timestamp": "2018-11-17T16:38:07.611Z",\n "value": 342\n },\n {\n "timestamp": "2018-11-16T16:38:07.611Z",\n "value": 498.9990428634777\n },\n {\n "timestamp": "2018-11-15T16:38:07.611Z",\n "value": 326\n },\n {\n "timestamp": "2018-11-14T16:38:07.612Z",\n "value": 649.9987532289786\n },\n {\n "timestamp": "2018-11-13T16:38:07.612Z",\n "value": 70\n },\n {\n "timestamp": "2018-11-12T16:38:07.612Z",\n "value": 349\n },\n {\n "timestamp": "2018-11-11T16:38:07.612Z",\n "value": 191\n },\n {\n "timestamp": "2018-11-10T16:38:07.612Z",\n "value": 154\n },\n {\n "timestamp": "2018-11-09T16:38:07.613Z",\n "value": 109\n },\n {\n "timestamp": "2018-11-08T16:38:07.613Z",\n "value": 237\n },\n {\n "timestamp": "2018-11-07T16:38:07.613Z",\n "value": 398\n },\n {\n "timestamp": "2018-11-06T16:38:07.613Z",\n "value": 606.9988357076774\n },\n {\n "timestamp": "2018-11-05T16:38:07.614Z",\n "value": 131\n },\n {\n "timestamp": "2018-11-04T16:38:07.614Z",\n "value": 397\n },\n {\n "timestamp": "2018-11-03T16:38:07.614Z",\n "value": 583.9988798241893\n },\n {\n "timestamp": "2018-11-02T16:38:07.614Z",\n "value": 362\n },\n {\n "timestamp": "2018-11-01T16:38:07.614Z",\n "value": 686.998682258936\n },\n {\n "timestamp": "2018-10-31T16:38:07.614Z",\n "value": 131\n },\n {\n "timestamp": "2018-10-30T16:38:07.614Z",\n "value": 212\n }\n]\nRun Code Online (Sandbox Code Playgroud)\n\n这些对象是使用以下代码创建的:
\n\n import { DateTime } from \'luxon\'\n\n const timestamp = startDate.minus({ days: i }).toJSDate()\n return { timestamp: timestamp, value: randomValue }\nRun Code Online (Sandbox Code Playgroud)\n\n我想要包含本月第一天的对象,所以在这个例子中,我想要:
\n\n {\n "timestamp": "2018-11-01T16:38:07.614Z",\n "value": 686.998682258936\n }\nRun Code Online (Sandbox Code Playgroud)\n\n这是我尝试过的:
\n\nconst date = new Date()\nconst firstDayOfThisMonth = new Date(date.getFullYear(), date.getMonth(), 1)\nconst firstDayOfThisMonthSub = firstDayOfThisMonth.toString().substring(0, 15)\nconst bo = dataset.map((d, i) => {\n const sub = d.toString().substring(0, 15)\n if (sub === firstDayOfThisMonthSub) return d\n})\nRun Code Online (Sandbox Code Playgroud)\n\n它不起作用(我得到一个数组undefined),我希望有一种更智能的方法来做到这一点。\n我可以使用 JavascriptDate对象或Luxon库。
谢谢
\ndio*_*uze 12
与勒克森:
const firstDayOfThisMonth = DateTime.local().startOf('month')
const firstDayRecord = dataset.find(record => {
return DateTime.fromISO(record.timestamp).hasSame(firstDayOfThisMonth, 'day')
})
Run Code Online (Sandbox Code Playgroud)
这应该可以解决问题