merge-most-frequent
v1.1.13
Published
JS library for merging objects like Object.assign but it takes the most frequent value instead of the last value
Downloads
33
Maintainers
Readme
merge-most-frequent
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