简体   繁体   English

过滤对象数组,其中对象属性为日期

[英]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.

相关问题 基于字符串属性过滤对象数组,但这是存储的日期,我需要返回所有对象,其中createdDate&gt; todaysDate - Filter an object array based on a string property but it is a date that is stored, and i need to return all objects where createdDate > todaysDate Javascript按对象的属性过滤对象数组 - Javascript filter an array of objects by a property of object 通过 object.property 将数组过滤为唯一对象 - Filter array to unique objects by object.property 如何在VueJS中按对象属性过滤对象数组 - How to filter array of objects by object property in VueJS 如何在JS中过滤对象具有属性tagId或keywordId的对象数组? - How to filter array of objects where object has property tagId or keywordId in JS? Javascript 过滤数组中的对象并返回数组中 Object 中 Object 的属性 - Javascript Filter Objects in Array and return Property of Object in Array of Object in Array 按属性过滤对象数组 - Filter array of objects by property 如何根据对象数组中属性的第一个字符过滤对象数组? - How to filter an array of objects based on the first character of a property in the array of object? 具有 Object 的对象过滤器数组驻留在嵌套数组属性中 - Filter Array of Objects with a Object reside in Nested Array property 过滤基于 object 的对象数组,其中数组作为其属性值在反应 - filter an array of objects based on an object with an array as it's property value in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM