UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

40 lines (39 loc) 943 B
import { getOrSet } from './get-or-set.js'; /** * Merges all arrays by their property in the given objects. * * @category Object : Merge * @category Package : @augment-vir/common * @example * * ```ts * import {mergePropertyArrays} from '@augment-vir/common'; * * mergePropertyArrays( * { * a: [ * 'a', * 'b', * ], * }, * { * a: [ * 'c', * 'd', * ], * }, * ); // output is `{a: ['a', 'b', 'c', 'd']}` * ``` * * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export function mergePropertyArrays(...inputs) { const combined = {}; inputs.forEach((input) => { Object.entries(input).forEach(([key, newArray,]) => { const currentArray = getOrSet(combined, key, () => []); currentArray.push(...newArray); }); }); return combined; }