UNPKG

arr-helper-functions

Version:

Misc. functions for finding elements in arrays, sorting, and more

27 lines (22 loc) 699 B
import { AccessorFunction } from './types'; /** * Gets the distinct values of an array * * @export * @param {any[]} array Array to find unique values of * @param {(AccessorFunction | string)} [property] Property to map or accesor function to use to obtain the desired value * @return {any[]} The unique array of values */ export function distinctValues( array: any[], property?: AccessorFunction | string, ): any[] { let arrayToFindDistinctOf = array; if (property != null) { const mapper = typeof property === 'function' ? property : (d: any): any => d[property]; arrayToFindDistinctOf = array.map(mapper); } return [...new Set(arrayToFindDistinctOf)]; }