-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch_object_of_arrayofobject_by_latestdate
More file actions
69 lines (64 loc) · 1.42 KB
/
fetch_object_of_arrayofobject_by_latestdate
File metadata and controls
69 lines (64 loc) · 1.42 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Fetch the object of the latest date from an array of objects
------------------------------------------------------------------------------
const objArr = [
{
"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
}
];
```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<p id="demo"></p>
<p id="demo1"></p>
<script>
const objArr = [
{
"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
}
];
const latestMeasureDate = objArr.map(obj => obj.MeasureDate).reduce((a, b) => (new Date(a) > new Date(b) ? a : b));
document.getElementById("demo").innerHTML = latestMeasureDate;
const finalObj = objArr.reduce((a, b) => (new Date(a.MeasureDate) > new Date(b.MeasureDate) ? a : b));
document.getElementById("demo1").innerHTML = JSON.stringify(finalObj);
</script>
</body>
</html>
```