@helcor/merge-collections

1.0.0-beta.9 • Public • Published

helcor

@helcor/merge-collections

version downloads

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);

Readme

Keywords

none

Package Sidebar

Install

npm i @helcor/merge-collections

Weekly Downloads

18

Version

1.0.0-beta.9

License

MIT

Unpacked Size

10.1 kB

Total Files

6

Last publish

Collaborators

  • romelperez