最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to return the lowest date value and highest date value from an array in Javascript? - Stack Overflow

programmeradmin0浏览0评论

How to return the lowest date value and highest date value from an array?

For example for the below array, I'd like to create a function that returns the lowest date, as well as another function that returns the highest date?

var data = [
  {date: "2011-11-01T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
  {date: "2011-11-14T16:20:19Z", quantity: 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, tip: 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, type: "cash"},
  {date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
  {date: "2011-11-31T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"}
]; 

I'm not sure if this is relevant, but I am using this array with crossfilter, so I'm not sure if crossfilter has any helpful methods.

How to return the lowest date value and highest date value from an array?

For example for the below array, I'd like to create a function that returns the lowest date, as well as another function that returns the highest date?

var data = [
  {date: "2011-11-01T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
  {date: "2011-11-14T16:20:19Z", quantity: 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, tip: 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, type: "cash"},
  {date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
  {date: "2011-11-31T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"}
]; 

I'm not sure if this is relevant, but I am using this array with crossfilter, so I'm not sure if crossfilter has any helpful methods.

Share Improve this question asked Nov 4, 2014 at 13:03 ChrisChris 5,83418 gold badges71 silver badges127 bronze badges 3
  • 1 Is the array always ordered like that? Becuase than you could simply return the first or the las – Vincent Beltman Commented Nov 4, 2014 at 13:07
  • 2 That last date doesn't convert to a date properly. – Andy Commented Nov 4, 2014 at 13:10
  • You can sort the array and then get the upper and lower bounds. Take a look at this answer: Sort Javascript Object Array By Date – jherax Commented Mar 30, 2015 at 23:24
Add a ment  | 

6 Answers 6

Reset to default 6

We just have to apply some Javascript Magic.

var result = Math.max.apply( null, data.map(function( v ) {
    return +new Date( v.date );
}));

There, we map those Objects into just their timestamp representation from the ISO-Date string. Then we simply apply a Math.max, respectively Math.min on the resulting Array. All we have left to do now, is to re-convert that timestamp into a Date object

new Date( result );

And just for the heck of it, an alternative solution using Array.prototype.reduce and pure String parison (if you are scared of browser inpatibilities on parsing ISO Date Strings).

var max = data.reduce(function( prev, current ) {
  return prev.date > current.date ? prev : current;
});
// for the min version just ">" to "<"

You can sort the array using a custom parator:

data.sort(function(o1,o2){
    return new Date(o1.date).getTime() - new Date(o2.date).getTime();
});

and then get the largest/smallest dates by getting the first/last items:

var smallest = data[0].date;
var largest  = data[data.length - 1].date;

A simple string parison should give you the expected result. Since the dates are in ISO string format, this should be fairly easy to do.

var lowestIndex = 0;
var lowestDate = data[0].date;
for (var i=0; i<data.length; i++) {
    if (lowestDate > data[i].date) {
        lowestDate = data[i].date;
        lowestIndex = i;
    }
}
return data[lowestIndex];

If you are fortable with apply and call, then you shouldn't have much trouble converting this simple logic to a function.

You could sort by those keys, and then take the first and last values:

sortedData = data.sort(function(a, b) {
    return new Date(a.date) > new Date(b.date) ? 1 : -1;
});
var min = sortedData[0],
    max = sortedData[sortedData.length - 1];

Try this:

var data = [
  {date: "2011-11-01T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
  {date: "2011-11-14T16:20:19Z", quantity: 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, tip: 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, type: "cash"},
  {date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
  {date: "2011-11-31T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"}
];

var lowest = data.reduce(function (a, b) {
    var timeA = new Date(a.date).getTime();
    var timeB = new Date(b.date).getTime();
    return timeA < timeB ? a : b;
});

alert("lowest: " + JSON.stringify(lowest, null, 4));

var highest = data.reduce(function (a, b) {
    var timeA = new Date(a.date).getTime();
    var timeB = new Date(b.date).getTime();
    return timeA > timeB ? a : b;
});

alert("highest: " + JSON.stringify(highest, null, 4));

Lots of suggestions to convert the strings to dates, however using the Date constructor to parse strings is unreliable and will fail, even for the format specified in ES5, for a reasonable number of browsers in use.

Since you are using ISO 8601 format date strings, you can sort them without conversion (that's one of the benefits of using that format, as well as its being unambiguous). So you can sort the array using:

data.sort(function(a, b) {return a.date > b.date? 1 : a.date < b.date? -1 : 0});

Now you just get the first and last member of the array:

var earliestDate = data[0].date;
var latestDate   = data[data.length - 1].date;

As others have said, the last date string is an invalid date, how should the sort deal with that? The above suggestion doesn't test whether the date is valid or not, it just looks at the characters.

发布评论

评论列表(0)

  1. 暂无评论