Reputation:
I am receiving this JSON object:
response.myDates = [ "2017-11-19T00:00:00.000Z", "2017-12-08T00:00:00.000Z", "2017-12-25T00:00:00.000Z", "2017-12-31T00:00:00.000Z", "2018-01-01T00:00:00.000Z" ]
I would like to save all these dates (actually there are hundreds of them) in a Date array parsedDates
in Javascript.
Is there a simple way to do it without a while loop?
Upvotes: 7
Views: 12062
Reputation: 26360
Just map over your array :
const myDates = [ "2017-11-19T00:00:00.000Z", "2017-12-08T00:00:00.000Z"],
datesArray = myDates.map( dateString => new Date(dateString) )
console.log(datesArray)
Note :
Surprisingly, this Stackoverflow snippet outputs an array of strings (not sure why), but if you run it in your Chrome console or in Codepen, it outputs an array of dates, as it should
Upvotes: 2
Reputation: 4292
You can simple do a map to new Date();
let results = response.myDates.map(date => new Date(date))
Upvotes: 10