Skip to main content
Module

x/ramda/uniqBy.js

:ram: Practical functional Javascript
Very Popular
Go to Latest
File
import _Set from './internal/_Set';import _curry2 from './internal/_curry2';

/** * Returns a new list containing only one copy of each element in the original * list, based upon the value returned by applying the supplied function to * each list element. Prefers the first item if the supplied function produces * the same value on two items. [`R.equals`](#equals) is used for comparison. * * @func * @memberOf R * @since v0.16.0 * @category List * @sig (a -> b) -> [a] -> [a] * @param {Function} fn A function used to produce a value to use during comparisons. * @param {Array} list The array to consider. * @return {Array} The list of unique items. * @example * * R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]); //=> [-1, -5, 2, 10] */var uniqBy = _curry2(function uniqBy(fn, list) { var set = new _Set(); var result = []; var idx = 0; var appliedItem, item;
while (idx < list.length) { item = list[idx]; appliedItem = fn(item); if (set.add(appliedItem)) { result.push(item); } idx += 1; } return result;});export default uniqBy;