How to return the lowest date value and highest date value from an array in Javascript? -
How will return the lowest date value and the highest date value from the array?
For example, below the array, I would like to create a function that gives at least a date, as well as another task that gives the highest date?
var data = [{date: "2011-11- 01T16: 17: 54Z", quantity: 2, total: 190, tip: 100, type: "tab"}, {date : "2011-11-14T16: 20: 19", volume: 2, total: 190, tip: 100, type: "tab"}, {date: "2011-11-14T16: 28: 54Z" Quantity: 1, Total: 300, Tip: 200, Type: "Visa"}, {Date: "2011- 11-14T16: 30: 43Z", Quantity: 2, Total: 90, Tip: 0, Type: "Tab"}, {date: "2011-11-14T16: 48: 46Z", quantity: 2, total: 90, tap: 0, type: "tab"}, {date: "2011-11-14T16: 53 : 41Z ", Quantity: 2, Total: 90, Tip: 0, Type:" Tab "}, {Date: 2011-11-14T16: 54: 06Z", Quantity: 1, Total: 100, Tip: 0 , Light : "Cash"}, {date: "2011-11-14T16: 58: 03Z", quantity: 2, total: 90, tip: 0, type: "tab"}, {date: "2011-11-14 tu 17: 07: 21Z ", Quant IT: 2, Total: 90, Tip: 0, Type:" Tab "}, {Date:" 2011-11-14 Tu 17: 22: 59Z ", Quantity: 2, Total: 90, device: 0, type: "tab"}, {date: "2011-11-14T17: 25: 45Z", quantity: 2, total: 200, tip: 0, type: "cash"}, {date: "2011-11-31 T17: 29: 52Z", Quantity: 1, Total: 200, Tip: 100, Type: "Visa"}];
I'm not sure that this is relevant, but I'm using this array with the crossfilter, so I'm not sure that crossfilter has any useful methods.
We just have to implement some javascript.
var results = Math.max.apply (empty, data.map (function (v) {return + new date (v.date);}));
There, we place those objects in our timestamp representation with the ISO date string. Then we apply the Math.min
respectively on the resulting array. Now what we have left to do, we have to convert that timestamp into a date object
new date (results);
And for just this, Array.prototype.readuce
and an alternative solution (if you are afraid of browser incompatibilities by using the pure string comparison. Date parsing strings).
var max = data.reduce (function (current, current) {return prev.date & gt; current.date? Prev: current;}); // just "& gt;" for the minimum version For "& lt;"
Comments
Post a Comment