geno/wp-content/plugins/woocommerce/packages/woocommerce-blocks/assets/js/utils/array-operations.ts

18 lines
465 B
TypeScript
Raw Permalink Normal View History

2024-02-01 17:24:18 +05:30
/**
* Returns the difference between two arrays (A - B)
*/
export function arrayDifferenceBy< T >( a: T[], b: T[], key: keyof T ) {
const keys = new Set( b.map( ( item ) => item[ key ] ) );
return a.filter( ( item ) => ! keys.has( item[ key ] ) );
}
/**
* Returns the union of two arrays (A B)
*/
export function arrayUnionBy< T >( a: T[], b: T[], key: keyof T ) {
const difference = arrayDifferenceBy( b, a, key );
return [ ...a, ...difference ];
}