2

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

3
  • What do you want? Either filter the array to get a subarray for a condition, or reduce it to a single value (which can be a complex object) Commented Oct 8, 2012 at 18:57
  • What you have done can be easier achieved with the groupBy function (demo). However, I did not really understand what you meant with that fuzzy reduction ("greater or less than date")? Commented Oct 8, 2012 at 19:07
  • I need to create some charts from the data so I was going to break it up into subarrays by time periods. e.g. 30,60,90,120... days from today. Since the current date will be used as the start date for the query, I guess I only need to be concerned with creating a subarray for each period. e.g. Subarray 1 - contains items with current date + 30 days Subarray 2 - contains items with current date + 60 days...etc..etc Commented Oct 8, 2012 at 20:08

1 Answer 1

1

If you want to filter the set, you can do this:

var reduced = data.filter(function(obj) {
    var date = +(new Date(obj.date));
    return date < someDate || date > someOtherDate
});

If you wanted to reduce it to a pair of sets, you can do this:

var now = Date.now();

var reduced = data.reduce(function(ret, obj) {
    var date = +(new Date(obj.date));
    if (date < now)
        ret.before.push(obj);
    else
        ret.onOrAfter.push(obj);
    return ret;
}, {before:[], onOrAfter:[]});
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the feedback here. I think I need your second example because that appears to enable me to break the data down into sub arrays based on the date ranges. I'm probably doing something stupid but I get Type error on this line: ret.onOrAfter.push(obj); TypeError: Cannot call method 'push' of undefined
@user1513388: I forgot to change the function name from filter to reduce. I'll update.
Arh - Nice one! Thanks that worked perfectly! I'm assuming that since this is using the JavaScript date functions providing the date can be parsed it does more than just a simple lexical string match on the dates?
@user1513388: Yes, it is converting the strings to Date objects, then converting to a number (ms since unix epoch).
Thanks one other thing! I was just trying a third (if else) option on the date e.g. "else if (date < now + 30);ret.thirtydays.push(obj);" but it's ignoring that line. Do I need to do something else with the "now" value in order to add 30 days to it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.