Below is my js object array.
const objArray = [
{
file: 'file_1',
start_time: '2021-08-12 14:00:00'
status: 'pending'
},
{
file: 'file_2',
start_time: '2021-08-12 14:00:00'
status: 'completed'
},
{
file: 'file_3',
start_time: '2021-08-14 15:00:00'
status: 'pending'
},
{
file: 'file_1',
start_time: '2021-08-14 03:00:00'
status: 'pending'
},
{
file: 'file_2',
start_time: '2021-08-14 03:00:00'
status: 'pending'
},
{
file: 'file_2',
start_time: '2021-11-11 11:11:00'
status: 'pending'
}
]
From above array, I need to filter the objects based on the start time field. If the start time are same they should be grouped as a sub array. Also within the sub array there can't be objects with same file name. Ex, In above array, if you compare objects 1&2 with 4&5, each of them have their own start time values, but their file names are same. Therefore I need only one set from them ie 1&2 which has the lowest timestamp. So the final output array should be as below,
[
[
{
file: 'file_1',
start_time: '2021-08-12 14:00:00'
status: 'pending'
},
{
file: 'file_2',
start_time: '2021-08-12 14:00:00'
status: 'completed'
}
],
[
{
file: 'file_3',
start_time: '2021-08-14 15:00:00'
status: 'pending'
}
],
[
{
file: 'file_2',
start_time: '2021-11-11 11:11:00'
status: 'pending'
}
]
]
I tried implement it by looping through every object from the initial array. But what the quickest way to achieve this?