Check out this code snippet:
list.component.ts
const data1 = [
{
dateStart: "2020-02-14 00:00:01",
name: 'Server1'
},
{
dateStart: "2020-02-13 14:00:01",
name: 'Server1'
},
{
dateStart: "2020-02-13 13:00:01",
name: 'Server1'
},
{
dateStart: "2020-02-13 12:00:01",
name: 'Server1'
},
{
dateStart: "2020-02-13 11:00:01",
name: 'Server1'
}
];
const data2 = [
{
dateStart: "2020-02-13 14:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-13 13:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-13 12:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-13 11:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-12 11:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-11 11:00:01",
name: 'Server2'
},
]
const arr = new Array();
arr.push(data1, data2);
arr.forEach(server => {
const rec = orderBy(server, ['dateStart'], ['desc']);
console.log(rec);
});
}
The aim is to only display the most recent entries from both data sets. Any duplicate dates should be eliminated in favor of older data.
Desired output:
[
{
dateStart: "2020-02-14 00:00:01",
name: "Server1"
},
{
dateStart: "2020-02-13 14:00:01",
name: "Server1"
},
{
dateStart: "2020-02-13 13:00:01",
name: "Server1"
},
{
dateStart: "2020-02-13 12:00:01",
name: "Server1"
},
{
dateStart: "2020-02-13 11:00:01",
name: "Server1"
}
],
[
{
dateStart: "2020-02-13 14:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-13 13:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-13 12:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-13 11:00:01",
name: 'Server2'
}
]
View the code snippet here: https://stackblitz.com/edit/angular-akhhbp
If a new array (data3) is introduced with more data, it will undergo similar comparison and filtering as data1 and data2.
Example scenario:
data1 = [{
dateStart: "2020-02-13 14:00:01",
name: 'Server1'
},
{
dateStart: "2020-02-13 13:00:01",
name: 'Server1'
},
{
dateStart: "2020-02-13 12:00:01",
name: 'Server1'
}]
data2 = [ {
dateStart: "2020-02-13 14:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-13 13:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-13 12:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-13 11:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-12 11:00:01",
name: 'Server2'
}],
data3 = [ {
dateStart: "2020-02-13 14:00:01",
name: 'Server3'
},
{
dateStart: "2020-02-13 13:12:01",
name: 'Server3'
},
{
dateStart: "2020-02-13 12:00:01",
name: 'Server3'
},
{
dateStart: "2020-02-13 11:00:01",
name: 'Server3'
},
{
dateStart: "2020-02-12 10:00:01",
name: 'Server3'
}]
Expected outcome:
[{
dateStart: "2020-02-13 14:00:01",
name: 'Server1'
},
{
dateStart: "2020-02-13 13:00:01",
name: 'Server1'
},
{
dateStart: "2020-02-13 12:00:01",
name: 'Server1'
}],
[ {
dateStart: "2020-02-13 14:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-13 13:00:01",
name: 'Server2'
},
{
dateStart: "2020-02-13 12:00:01",
name: 'Server2'
}],
[ {
dateStart: "2020-02-13 14:00:01",
name: 'Server3'
},
{
dateStart: "2020-02-13 13:12:01",
name: 'Server3'
},
{
dateStart: "2020-02-13 12:00:01",
name: 'Server3'
},
{
dateStart: "2020-02-13 11:00:01",
name: 'Server3'
},
{
dateStart: "2020-02-12 10:00:01",
name: 'Server3'
}]