[英]Filter array of objects where object property is date
I have an array of objects where each object looks like this 我有一个对象数组,每个对象看起来像这样
{
"ActionDateTime": "2018-07-31T11:07:11Z",
"ExternalID": 3962770,
"GrandTotal": 707.5,
"InternalID": 52858173,
"ItemName": "Ricciolini Pasta",
}
I need to filter this array (maybe using moment.js) in such a way that "ActionDateType" will be equal to current Month (moment().format('MMMM') //August) 我需要以“ ActionDateType”等于当前月份(moment()。format('MMMM')//八月)的方式过滤此数组(也许使用moment.js)
This is my code: 这是我的代码:
var array = []; array = data.filter(function (el) { var a = el.ActionDateTime return moment(a).format('MMMM') == moment().format('MMMM') }) console.log(array)
function timeTest() { let dateStr = '2018-07-31T11:07:11Z' let dateTs = new Date(dateStr) let ret = dateTs.getFullYear().toString() + '.' + (dateTs.getMonth() + 1).toString() + '.' + dateTs.getDate().toString() console.log(ret) } timeTest()
I think you can use javascript's api. 我认为您可以使用javascript的api。
You can use JavaScripts Date
to do that. 您可以使用JavaScript
Date
来做到这一点。
Since your ActionDateTime
value is in a parsable format, you can use Date.parse
, to get a timestamp. 由于您的
ActionDateTime
值采用可解析的格式,因此可以使用Date.parse
来获取时间戳。
let date = Date.parse ("2018-07-31T11:07:11Z")
To get the current month, you can use new Date().getMonth ()
. 要获取当前月份,可以使用
new Date().getMonth ()
。
Combining those too, you get something like this. 结合这些,您将得到类似的结果。
let arr = [{ "ActionDateTime": "2018-08-31T11:07:11Z", //<-- Note that i changed 07 to 08 "ExternalID": 3962770, "GrandTotal": 707.5, "InternalID": 52858173, "ItemName": "Ricciolini Pasta", }] let tempDate = new Date (); //Initialise a new Date let currentMonth = tempDate.getMonth (); //Get the current month, from the untouched date. let filtered = arr.filter (({ActionDateTime}) => tempDate.setTime (Date.parse (ActionDateTime)) && tempDate.getMonth () === currentMonth); console.log (filtered)
As pointed out in the comments: i changed the Date such that the filter returns an entry when run in August. 正如评论中指出的那样:我更改了日期,以便过滤器在8月运行时返回一个条目。
I have modified OP's code (which was certainly working, but I needed to remove the moment dependency for it to work here). 我已经修改了OP的代码(这确实可以正常工作,但是我需要删除对时间的依赖性才能使其在这里工作)。
What you were doing was right, but you need to filter data from current month while your data is from last month. 您所做的正确,但是您需要过滤当月的数据,而您要过滤上个月的数据。 I've added 3 data, 1 of last month, 2 of current month but one from last year.
我添加了3个数据,上个月1个,当月2个,但去年一个。 Commented out the return statement that accounts for the year too, not sure if it is wanted but I expect it was just forgotten.
注释掉了也占这一年的收益表,不确定是否需要,但我希望它只是被忘记了。
var data = [ { "ActionDateTime": "2018-07-31T11:07:11Z", "ExternalID": 3962770, "GrandTotal": 707.5, "InternalID": 52858173, "ItemName": "Ricciolini Pasta", }, { "ActionDateTime": "2018-08-02T11:07:11Z", "ExternalID": 3962770, "GrandTotal": 707.5, "InternalID": 52858173, "ItemName": "Ricciolini Pasta", }, { "ActionDateTime": "2017-08-02T11:07:11Z", "ExternalID": 3962770, "GrandTotal": 707.5, "InternalID": 52858173, "ItemName": "Ricciolini Pasta", }]; let array = []; let curDate = new Date(); array = data.filter(function (el) { let a = new Date(el.ActionDateTime) //return a.getMonth() ==curDate.getMonth() && a.getYear() ==curDate.getYear() return a.getMonth() ==curDate.getMonth() }) console.log(array)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.