I am working with an array that looks like this:
[{
"res": [ '123', 'One', '20210318' ]
}, {
"res": [ '123', 'One', '20210319' ]
}, {
"res": [ '123', 'One', '20210320' ]
}, {
"res"': [ '123', 'One', '20210321' ]
},{
"res"': [ '456', 'Two', '20210401' ]
},{
"res"": [ '456', 'Two', '20210402' ]
},{
"res": [ '456', 'Two', '20210403' ]
},{
"res": [ '456', 'Two', '20210404' ]
}]
My goal is to extract the following unique values from the array:
[{
"res": [ '123', 'One', '20210318' ]
}, {
"res": [ '123", "One", "20210321' ]
},{
"res": [ '456', 'Two', '20210401' ]
},{
"res": [ '456', 'Two', '20210404' ]
}]
This means finding the first occurrence of unique value "20210318" for "One" and the last index unique value "20210321", and for "Two" finding the first unique value as "20210401" and the last index unique value as "20210404"
So far, I have successfully filtered the array by their unique IDs and obtained only two results - the first unique occurrence of "One" and the first unique occurrence of "Two"
const getFilteredArray = array.filter((v,i,a)=> {
const getFilter = a.findIndex( t => {
return t.res[0] === v.res[0]
}) === i;
return getFilter;
})