Skip to content

Home > sculp-js > diffArray

diffArray() function

Compare source array and target array, return diff result (added / removed).

  • If getKey is not provided: - Primitive values (string | number | symbol) will be used as keys directly. - If getKey is provided: - It will be used to extract unique keys from items.

T

Signature:

typescript
export declare function diffArray<T>(source: readonly T[], target: readonly T[], getKey?: GetKey<T>): DiffResult<T>;

Parameters

Parameter

Type

Description

source

readonly T[]

Source array (original data)

target

readonly T[]

Target array (new data)

getKey

GetKeyT

(Optional) Optional function to get unique key

Returns:

DiffResultT

DiffResult<T>

Example 1

ts
diffArray([1, 2, 3], [2, 3, 4])
// => { added: [4], removed: [1] }

Example 2

ts
diffArray(['a', 'b'], ['b', 'c'])
// => { added: ['c'], removed: ['a'] }

Example 3

ts
diffArray(
  [{ id: 1 }, { id: 2 }],
  [{ id: 2 }, { id: 3 }],
  item => item.id
)
// => { added: [{ id: 3 }], removed: [{ id: 1 }] }

Released under the MIT License.