Let’s say I have an array of objects, like so:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
let trades = [
  {
    id: 1,
    date: "2019-04-23T18:25:43.511Z",
    asset: "BTC",
    orderType: "Limit Buy",
    platform: "Coinhako",
    price: 46580,
    price_denom: "USD",
    nominal_price: 32313 ,
    amt: 0.003,
    value: 139.74,
    value_denom: "USD",
    fees: 4,
    fees_denom: "USD",
    comments: "good buy"
  },
  {
    id: 2,
    date: "2019-05-23T18:25:43.511Z",
    asset: "BTC",
    orderType: "Limit Buy",
    platform: "Gemini",
    price: 38000,
    price_denom: "USD",
    nominal_price: 32313,
    amt: 0.02,
    value: 760,
    value_denom: "USD",
    fees: 14,
    fees_denom: "USD",
    comments: "not bad buy"
  },
  {
    id: 3,
    date: "2019-05-23T18:25:43.511Z",
    asset: "Alibaba",
    orderType: "Limit Buy",
    platform: "SC",
    price: 196,
    price_denom: "HKD",
    nominal_price: 34.07,
    amt: 100,
    value: 19600,
    value_denom: "HKD",
    fees: 128,
    fees_denom: "HKD",
    comments: "bad buy"
  }
]

I want to get the latest date.

The following solution will do the trick:

1
const latestDate = new Date(Math.max(...trades.map(trade => new Date(trade.date))))

...trades.map is the property spread notation. It ‘spreads’ out an array into a list of arguments. Basically removes the square brackets.

In my case, trades.map(trade => new Date(trade.date)) will return an array of dates. Literally this: [Date Wed Apr 24 2019 02:25:43 GMT+0800 (Singapore Standard Time), 1: Date Fri May 24 2019 02:25:43 GMT+0800 (Singapore Standard Time)]

The ... before, will convert this array into simply 2 arguments, which you pass into Math.max() which will return you the latest. Then you of course create a new date out of it.

Brilliant.