merge-most-frequent

JS library for merging objects like Object.assign but it takes the most frequent value instead of the last value

Usage no npm install needed!

<script type="module">
  import mergeMostFrequent from 'https://cdn.skypack.dev/merge-most-frequent';
</script>

README

merge-most-frequent

MIT License Build Status

NPM status

JS library for merging objects like Object.assign but it takes the most frequent value instead of the last value

how to install?

yarn add merge-most-frequent or npm i --save merge-most-frequent

how to use?

const mergeMostFrequent = require('merge-most-frequent').default;
/*
 * or:
 * import mergeMostFrequent from 'merge-most-frequent';
 */

const objects = [
  {
    foo: 'foo1',
    bar: 'bar1',
    baz: 'baz1',
  },
  {
    foo: 'foo1',
    bar: 'bar2',
    baz: 'baz2',
  },
  {
    foo: 'foo2',
    bar: 'bar2',
    baz: 'baz1',
  },
];

expect(mergeMostFrequent(objects)).to.deep.equal({
  foo: 'foo1',
  bar: 'bar2',
  baz: 'baz1',
});

how does it work?

  • for each field, it returns the most frequent value
  • if many values occur for the same number of times, the most recently used value wins
  • a field has to occur in at least one of the objects, to be included in the result