1.0.0-beta.9 • Published 4 years ago

@helcor/merge-collections v1.0.0-beta.9

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

helcor

@helcor/merge-collections

Merge two arrays of objects by identifier.

API

mergeCollections(array1, array2, options)

  • Array array1 - Source collection.
  • Array array2 - Collection to merge.
  • Object options - Options. (Optional)
    • string id - The identifier. Default: 'id'. (Optional)
    • boolean shallow - Shallow extend. Default: false. (Optional)
  • Returns Array - A new collection with the merged items.

Example

import mergeCollections from '@helcor/merge-collections';

const col1 = [
  { myId: 'x1', v: 'a' },
  { myId: 'x2', v: 'b' },
  { myId: 'x3', v: 'c' }
];
const col2 = [
  { myId: 'x1', v: 'x' },
  { myId: 'x2', v: 'y' },
  { myId: 'x4', v: 'z' }
];
const received = mergeCollections(col1, col2, { id: 'myId' });
const expected = [
  { myId: 'x1', v: 'x' },
  { myId: 'x2', v: 'y' },
  { myId: 'x3', v: 'c' },
  { myId: 'x4', v: 'z' }
];
expect(received).toEqual(expected);