user1094081
user1094081

Reputation:

Underscore.js, set the current date to keys containing past dates

I've a large array (JSON coming from a server request) and inside this array I've lots of JSON objects. One of the keys contains a date in ISO format. I would like to change the value of this key, so that if the date is past or equals today, all the keys containing a past or current date changes its value to the current date (today), while the future (and the current) dates stay the same.

So, for example, here:

({
 "date_upd": "2015-02-05T19:11:56.520Z"
 },
{
 "date_upd": "2015-03-08T19:12:56.520Z"
 },
{
 "date_upd": "2015-02-05T19:11:56.520Z"
 })

The first two keys should change to 2015-04-08.

Is this possible with underscore?

Upvotes: 0

Views: 523

Answers (1)

Nicholas Thomson
Nicholas Thomson

Reputation: 548

Try this:

var today = new Date().toISOString();
_.each(your_array, function(item){
    item.date_upd = item.date_upd < today ? today : item.date_upd;
});

your_array will be updated in place. So after the each call completes, the changes should be reflected in your_array.

Upvotes: 1

Related Questions