GuobaGuoba Utils

diff

@guoba-ai/utils / array / diff

Function: diff()

function diff<T>(
   a, 
   b, 
   fn?): T[];

Defined in: array.ts:410

Return items in the first array that are not in the second array.

Parameters

a

T[]

The source array

b

T[]

The array of items to exclude

fn?

(item) => unknown

Optional function to map items before comparison

Returns

T[]

A new array of items in a but not in b

Example

diff([1, 2, 3, 4], [2, 4]) // [1, 3]

diff([{ id: 1 }, { id: 2 }], [{ id: 2 }], item => item.id)
// [{ id: 1 }]

diff([{ id: 1 }], [{ id: 1 }]) // [{ id: 1 }]

Warning

Without fn, object values are compared by reference, not by shape.

On this page