I have an array of items in Javascript similar to the following:
var data =
[
{"id":"338b79f07dfe8b3877b3aa41a5bb8a58","date":"2000-10-05T13:21:30Z","value": {"country":"United States"}},
{"id":"338b79f07dfe8b3877b3aa41a5bb983e","date":"2000-02-05T13:21:30Z","value":{"country":"Norway"}},
{"id":"338b79f07dfe8b3877b3aa41a5ddfefe","date":"2000-12-05T13:21:30Z","value":{"country":"Hungary"}},
{"id":"338b79f07dfe8b3877b3aa41a5fe29d7","date":"2000-05-05T13:21:30Z","value":{"country":"United States"}},
{"id":"b6ed02fb38d6506d7371c419751e8a14","date":"2000-05-05T18:15:30Z","value":{"country":"Germany"}},
{"id":"b6ed02fb38d6506d7371c419753e20b6","date":"2000-12-05T18:15:30Z","value":{"country":"Hungary"}},
{"id":"b6ed02fb38d6506d7371c419755f34ad","date":"2000-06-05T18:15:30Z","value":{"country":"United States"}},
{"id":"b6ed02fb38d6506d7371c419755f3e17","date":"2000-04-05T22:15:30Z","value":{"country":"Germany"}},
{"id":"338b79f07dfe8b3877b3aa41a506082f","date":"2000-07-05T22:15:30Z","value":{"country":"United Kingdom"}},
{"id":"9366afb036bf8b63c9f45379bbe29509","date":"2000-11-05T22:15:30Z","value":{"country":"United Kingdom"}}
];
I need to query (reduce) the array by the date. Either greater than or less than a pre-determined date eg. current date.
I was thinking of using Underscores reduce method to do this. Can anybody provide an example of how I could do this?
Edit: I trying something like this:
var itemsByDate = _(items).reduce(function(memo, item) {
memo[item.date] = memo[item.date] || [];
memo[item.date].push(item);
return memo;
}, {});
console.log((JSON.stringify(itemsByDate["2000-11-05T22:15:30Z"])));
But this looks for an exact match and will probably not deal with the dates properly because they are strings.
Regards,
Carlskii
If you want to filter the set, you can do this:
If you wanted to reduce it to a pair of sets, you can do this: