2024-04-25 14:56:13 +03:00
# differenceWith
Computes the difference between two arrays based on a custom equality function.
2024-06-04 11:19:26 +03:00
This function takes two arrays and a custom comparison function. It returns a new array containing
the elements that are present in the first array but not in the second array. The comparison to determine
2024-04-25 14:56:13 +03:00
if elements are equal is made using the provided custom function.
## Signature
```typescript
2024-06-04 11:19:26 +03:00
function differenceWith< T > (firstArr: T[], secondArr: T[], areItemsEqual: (x: T, y: T) => boolean): T[];
2024-04-25 14:56:13 +03:00
```
### Parameters
- `firstArr` (`T[]`): The array from which to get the difference.
2024-06-04 11:19:26 +03:00
- `secondArr` (`T[]`) :The array containing elements to exclude from the first array.
2024-04-25 14:56:13 +03:00
- `areItemsEqual` (`(x: T, y: T) => boolean`): A function to determine if two items are equal.
### Returns
2024-06-04 11:19:26 +03:00
(`T[]`) A new array containing the elements from the first array that do not match any elements in the second array according to the custom equality function.
2024-04-25 14:56:13 +03:00
## Examples
```typescript
import { differenceWith } from 'es-toolkit/array';
const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
const array2 = [{ id: 2 }, { id: 4 }];
const areItemsEqual = (a, b) => a.id === b.id;
const result = differenceWith(array1, array2, areItemsEqual);
// result will be [{ id: 1 }, { id: 3 }] since the elements with id 2 are considered equal and are excluded from the result.
```