Utilizing the moment library, my array is structured as follows:
data:
[
{
"id": "610",
"description": "New Test",
"start": "2021-08-04T14:20:00.000Z",
"end": "2021-08-04T15:30:00.000Z",
"profile": {
"firstName": "Steve",
"lastName": "Tene"
}
},
{
"id": "610b",
"description": "test",
"start": "2021-08-03T13:30:00.000Z",
"end": "2021-08-03T14:30:00.000Z",
"profile": {
"firstName": "Steve"
}
},
]
I attempted the following code snippet, but it seems that the map function only iterates through the array without making any changes.
data.content.map(x=>moment(x.start).toDate())
How can I create a loop to effectively convert the start and end strings into actual time values?
Edit: After trying out the solutions proposed by all of you, thanks for the prompt responses. Unfortunately, I encountered this error whenever I attempted anything, so I decided to provide more code snippets.
Error: Cannot assign to read only property 'start' of object '#'
const [schedule, setSchedule] = useState([])
useEffect(() => {
const fetchData = async () => {
const { searchavailable: data } = await searchAvailability()
console.log(data.content)
if (data) {
data.content = data.content.forEach(x => {
x.start = moment(x.start).toDate();
x.end = moment(x.end).toDate();
});
setSchedule(data.content)
}
}
fetchData()
}, [schedule])
FIXED!!!!
Thanks to everyone for your valuable comments, with the use of deepClone from lodash, I was able to resolve the issue!