param arr1 param arr2 param cb export function mergeBy < T > ( arr1 , arr2 , cb ) { const groupedArr1 = groupBy ( arr1 , cb ) ; const groupedArr2 = groupBy ( arr2 , cb ) ; const result = Object . values ( groupedArr2 ) . map ( ( value ) => value [ value . length - 1 ] ) ; const arr2Keys = Object . keys ( groupedArr2 ) ; Object . entries ( groupedArr1 ) . forEach ( ( [ key , value ] ) => { if ( ! arr2Keys . includes ( key ) ) { result . push ( value [ value . length - 1 ] ) ; } } ) ; return result ; } const groupBy = < T > ( arr , cb ) : { [ key ] : T [ ] ; } => { return arr . reduce < { [ key ] : T [ ] ; } > ( ( acc , curr ) => { const key = cb ( curr ) ; if ( ! acc [ key ] ) { acc [ key ] = [ ] ; } acc [ key ] . push ( curr ) ; return acc ; } , { } ) ; } ;