Function: difference()
ts
function difference<T>(arr, values?): T[];Returns an array of values from the first array that are not present in the second array. 返回第一个数组中不存在于第二个数组中的值组成的数组。
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the array elements / 数组元素的类型 |
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
arr | T[] | undefined | The array to inspect / 要检查的数组 |
values | T[] | [] | The values to exclude from the result / 要从结果中排除的值 |
Returns
T[]
Array of values not present in the exclusion array / 不存在于排除数组中的值组成的数组
Example
typescript
const numbers = [1, 2, 3, 4, 5]
const toExclude = [3, 4, 5, 6, 7]
difference(numbers, toExclude) // => [1, 2] *
const fruits = ['apple', 'banana', 'orange', 'grape']
const citrus = ['orange', 'lemon', 'lime']
difference(fruits, citrus) // => ['apple', 'banana', 'grape'] *
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' }
]
const activeUsers = [{ id: 2, name: 'Bob' }]
difference(users, activeUsers) // => [{ id: 1, name: 'Alice' }, { id: 3, name: 'Charlie' }] *
// Empty exclusion array returns original array
difference([1, 2, 3], []) // => [1, 2, 3] *
// No matches returns original array
difference([1, 2, 3], [4, 5, 6]) // => [1, 2, 3]Since
1.0.0