Javascript – Enumerating key, value pairs in object and converting those that match ISO timestamp format

Overview: I have an array (arr) of objects. I need to convert any value in an object that looks like an ISO timestamp to an epoch. I match via regular expression.

I have an object that looks like:

{
    assignee: null
    color: null
    description: "cool object"
    id: "12234858"
    last_updated: "2021-01-22T15:30:10.495000+00:00"
}

I have some code that does the following:

arr.forEach((item) => {
    let regex = /^[+-]?\d{4}(-[01]\d(-[0-3]\d(T[0-2]\d:[0-5]\d:?([0-5]\d(\.\d+)?)?[+-][0-2]\d:[0-5]\dZ?)?)?)?/;
    Object.entries(myobject).forEach(([key, value]) => {
        if (value !== null) {
            if (value.match(regex)) {
                value = Date.parse(value) / 1000;
            }
        }
    });
});

The date conversions work while iterating through the key, values of each object, but once the arr.forEach() completes, the values are reset. What is the best way to handle these conversions? Should I map to new object and duplicate?

142 thoughts on “Javascript – Enumerating key, value pairs in object and converting those that match ISO timestamp format”

  1. What i do not realize is in truth how you are now not actually a lot more smartly-appreciated than you may be
    right now. You’re so intelligent. You already know therefore considerably
    when it comes to this topic, made me in my view consider it from a lot of numerous angles.
    Its like women and men don’t seem to be interested until it is something to
    do with Girl gaga! Your own stuffs excellent. Always maintain it up! http://antiibioticsland.com/Erythromycin.htm

    Reply
  2. Simply want to say your article is as amazing. The clearness in your
    put up is simply spectacular and i could suppose you’re a professional in this subject.
    Well with your permission let me to grasp your RSS feed to stay up
    to date with approaching post. Thank you 1,000,000 and please continue the enjoyable work.

    Reply
  3. Hi, I think your site might be having browser compatibility issues.
    When I look at your blog site in Chrome, it looks fine
    but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that,
    fantastic blog!

    Reply
  4. Greate post. Keep writing such kind of info on your site.

    Im really impressed by your blog.
    Hello there, You have performed an excellent job.
    I’ll definitely digg it and in my view recommend to my friends.

    I’m confident they will be benefited from this
    web site.

    Reply
  5. Excellent blog here! Also your web site loads up very fast!
    What web host are you using? Can I get your affiliate link to
    your host? I wish my website loaded up as quickly as yours lol

    Reply
  6. Thank you a bunch for sharing this with all folks you really
    know what you are speaking about! Bookmarked. Please additionally discuss with my
    site =). We can have a link exchange agreement between us

    Reply
  7. Aw, this was a really good post. Spending some time and actual
    effort to create a top notch article… but what can I say… I put things off a lot and never seem to get nearly
    anything done.

    Reply