So I have an array of objects from the api, which looks like this:
[{
"title": "demo",
"eventType": "demo",
"start": "2022-06-26T07:00:00.000Z",
"end": "2022-06-26T04:00:00.000Z"
...
},{}
]
I would like to create a new array with the same values but wrapping the start and end object values in a new Date() like this:
[{
"title": "demo",
"eventType": "demo",
"start": new Date("2022-06-26T07:00:00.000Z"),
"end": new Date("2022-06-26T04:00:00.000Z")
...
},{}
]
>Solution :
Use the map method on the array to loop over each object and return a modified object.
const result = data.map(({ start, end, ...rest }) => ({
...rest,
start: new Date(start),
end: new Date(end)
}));