Returns an object containing the added and removed items between two arrays.
Parameters:
| Name | Type | Description |
|---|---|---|
fromArr | Array | The from array. |
toArr | Array | The to array. |
key | string | | The key to use for the comparison. If a function is provided, it will be used to get the key. |
- Since
- 4.0.7
- Source
Returns:
An object containing the added and removed items.
- Type:
- object
Examples
const fromArr = [ 1, 2, 3 ];
const toArr = [ 1, 2, 3, 4, 5 ];
const diff = arrayDiff( fromArr, toArr );
console.log( diff );
// { added: [ 4, 5 ], removed: [] }const fromArr = [ { id: 1, name: 'John' }, { id: 2, name: 'Jane' } ];
const toArr = [ { id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Joe' } ];
const diff = arrayDiff( fromArr, toArr, 'id' );
console.log( diff );
// { added: [ { id: 3, name: 'Joe' } ], removed: [] }