I want to be able to return a result set of data and just change the formatting of the date field to something more readable leaving all the other data intact. I would prefer t
you can make a generic function which take array of objects and return array with modified-props objects using map()
and Object.assign()
.
const users = [
{ name: 'Jhon', logged: 'Tue Feb 04 2020 14:16:10 GMT+0200 (Eastern European Standard Time)' },
{ name: 'Doe', logged: 'Tue Feb 04 2020 14:20:10 GMT+0200 (Eastern European Standard Time)' }
];
const handleDates = (list, prop) => {
return list.map(item => {
const obj = Object.assign({}, item);
obj[prop] = new Date(obj[prop]).toLocaleDateString();
return obj;
});
}
console.log(users)
console.log(handleDates(users, 'logged'))