Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I update / edit the value of objects in an array of objects?

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")
...
},{}
]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
}));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading