简体   繁体   中英

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)

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.

You can use JavaScripts Date to do that.

Since your ActionDateTime value is in a parsable format, you can use Date.parse , to get a timestamp.

let date = Date.parse ("2018-07-31T11:07:11Z")

To get the current month, you can use 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.

I have modified OP's code (which was certainly working, but I needed to remove the moment dependency for it to work here).

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. 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM