-1

I have an array with same cohort_id, stats_type and service_date_end

[
  {
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-04 11:20:20'
  },
  {
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-04 09:20:20'
  },
  {
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-04 09:19:20'
  },
  {
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-03 09:19:20'
  },
  {
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2021-01-01',
    calculation_time: '2021-01-01 09:19:20'
  },
  {
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-01 09:19:20'
  },
  {
    cohort_id: '1',
    stats_type: 'diagnostic',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-03 09:19:20'
  },
  {
    cohort_id: '1',
    stats_type: 'diagnostic',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-02 09:19:20'
  }
]

for every identical stats type and service date end i would like to have only those records with the most current calculation time and eliminate other data from the array How do I accomplish that in JavaScript?

Bot _Vvk
  • 3
  • 3
  • Please, provide the code where you attempted to solve the problem – Aziz Hakberdiev Jul 27 '22 at 09:34
  • sort by calculaton date https://stackoverflow.com/questions/10123953/how-to-sort-an-object-array-by-date-property and https://stackoverflow.com/questions/2218999/how-to-remove-all-duplicates-from-an-array-of-objects – cmgchess Jul 27 '22 at 09:35

1 Answers1

0

You can use reduce and Object.values for filter it same as :

const data = [{
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-04 11:20:20'
  },
  {
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-04 09:20:20'
  },
  {
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-04 09:19:20'
  },
  {
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-03 09:19:20'
  },
  {
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2021-01-01',
    calculation_time: '2021-01-01 09:19:20'
  },
  {
    cohort_id: '1',
    stats_type: 'comprehensive',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-01 09:19:20'
  },
  {
    cohort_id: '1',
    stats_type: 'diagnostic',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-03 09:19:20'
  },
  {
    cohort_id: '1',
    stats_type: 'diagnostic',
    service_date_end: '2020-11-30',
    calculation_time: '2021-01-02 09:19:20'
  }
]

const result = Object.values(
  data.reduce((res, ele) => {
    let exist = res[ele.stats_type + ele.service_date_end] ??= {...ele}
    if(exist.calculation_time < ele.calculation_time) exist.calculation_time = ele.calculation_time
    return res
  }, {})
)
console.log(result)

with ??= document

The logical nullish assignment (x ??= y) operator only assigns if x is nullish (null or undefined).

Xupitan
  • 1,601
  • 1
  • 8
  • 12