1

I have an array of hashes like this:

[
 {
  "Address": 25,
  "AlertType": 1,
  "Area": "North",
  "MeasureDate": "2019-02-01T00:01:01.001Z",
  "MeasureValue": -1
 },
{
  "Address": 26,
  "AlertType": 1,
  "Area": "West",
  "MeasureDate": "2016-04-12T15:13:11.733Z",
  "MeasureValue": -1
},
{
  "Address": 25,
  "AlertType": 1,
  "Area": "North",
  "MeasureDate": "2017-02-01T00:01:01.001Z",
  "MeasureValue": -1
}
      .
      .
      .
]

And I need to find the most recent date but return the Address from the corresponding hash. I've got the code that finds the newest date:

let newest = new Date(Math.min.apply(null, data.map(function(e) {
  return new Date(e.created_at);
})));
most_recent = newest;

But I can't seem to get the address to return. The code I've written just returns the date or address from the

let findEarliest = (name) => {
  let nameIndex = 0;
  data.forEach((date) => {
    if(date.created_at.includes(most_recent));
      nameIndex = data.indexOf(date);
  });
  return data[nameIndex].name;
};

Any help would be appreciated.

2 Answers2

1

Try this out. Sorts the hash array by MeasureDate and then returns the first element's Address.

const hash = [
   {
  "Address": 1,
  "AlertType": 1,
  "Area": "North",
  "MeasureDate": "2010-02-01T00:01:01.001Z",
  "MeasureValue": -1
 },
 {
  "Address": 25,
  "AlertType": 1,
  "Area": "North",
  "MeasureDate": "2019-02-01T00:01:01.001Z",
  "MeasureValue": -1
 },
{
  "Address": 26,
  "AlertType": 1,
  "Area": "West",
  "MeasureDate": "2016-04-12T15:13:11.733Z",
  "MeasureValue": -1
},
{
  "Address": 20,
  "AlertType": 1,
  "Area": "North",
  "MeasureDate": "2017-02-01T00:01:01.001Z",
  "MeasureValue": -1
}
]

const latestAddress = hash.sort((a, b) => b.MeasureDate > a.MeasureDate)[0].Address

console.log(latestAddress)
RobG
  • 142,382
  • 31
  • 172
  • 209
Christopher Messer
  • 2,040
  • 9
  • 13
  • Getting undefined with that. – Mike Alexander Aug 17 '17 at 05:58
  • If you run the code snippet in SO - it works for me. Are you running the code snippet in SO or trying it elsewhere? Are there any differences between the two? – Christopher Messer Aug 17 '17 at 05:59
  • I was trying it in my interpreter. The data is slightly different but I updated your code accordingly. The array is called data and the dates are called created_at. I actually found the data that I posted above here on Stack and used it because the date format matched. The only other thing I can think of is that the keys aren't strings. They're in this format... address: .. could that be it? – Mike Alexander Aug 17 '17 at 06:04
  • The keys in the objects must be strings, unless they're a [*keyed collection*](http://ecma-international.org/ecma-262/8.0/#sec-keyed-collection) like *Map* or *Set*, etc. – RobG Aug 17 '17 at 06:09
  • This sort function should be `a.MeasureDate.localCompare(b.MeasureDate)`. As it is, the function will only return true or false, which coerce to 1 or 0 respectively. There is no -1 so `a == b` and `a > b` are sorted as equivalent. – RobG Aug 17 '17 at 06:19
  • Cool. Thanks for the assist! – Mike Alexander Aug 17 '17 at 17:06
0

You can use this code:

Updated 17/08/2017:

RobG warned me. This code is the best way right now

Best way:

const lastAddresDetails = addressList.sort((a, b) => b.MeasureDate.localeCompare(a.MeasureDate))[0];

console.log(lastAddresDetails.Address)

Other ways

const lastAddresDetails = addressList.sort((a, b) => b.MeasureDate > a.MeasureDate)[0];

console.log(lastAddresDetails.Address)

or

var lastAdressDetails = addressList.sort(function (a, b) {
            return b.MeasureDate > a.MeasureDate;
        })[0];

console.log(lastAdressDetails.Address);
Ali
  • 1,358
  • 3
  • 21
  • 32
  • This treats `a == b` and `a > b` as equal in the sort, so will not reliably return the smallest value. You should use [*localeCompare*](http://ecma-international.org/ecma-262/8.0/#sec-properties-of-the-string-prototype-object) which is specifically designed for ordering strings. See [*Sort array of objects by string property value in JavaScript*](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript). – RobG Aug 17 '17 at 06:22