Skip to content

Mapping Over an Array and Converting Date Property to Milliseconds and Returning New Array

I want to iterate over an array of objects, convert the date property to milliseconds, and then end up with a new array, with the same properties as the first array, but with the date property changed to milliseconds.

So for this:

let records = [
  {id: 1, date: '29/06/2020'},
  {id: 2, date: '29/07/2020'},
  {id: 3, date: '29/08/2020'}

I should end up with:

  {id: 1, date: 1593403200000}
  {id: 2, date: 1595995200000}
  {id: 3, date: 1598673600000}

I was using map like so:

 let newRecords = => = moment(, 'DD/MM/YYYY').valueOf());

… but it returns an array of objects containing ONLY the date property, like so:


… whereas I want the original objects, just with the date converted to milliseconds. How can I edit my map() function to return the entire original object in the new array?


map will return a new array of values you return from the callback function. So in your case you should return the object and not the date value.

let newRecords = => {
    const date = moment(, 'DD/MM/YYYY').valueOf());
    return {...r, date}; // using spread operator just to get a new reference of the object