Debian Codegraph @

addons/point_of_sale/static/src/app/models/utils/unique_by.js

raw
1 /* Returns an array containing all elements of the given
2 * array corresponding to the rule function {agg} and without duplicates
3 *
4 * @template T
5 * @template F
6 * @param {T[]} array
7 * @param {F} function
8 * @returns {T[]}
9 */
10 export function uniqueBy(array, agg) {
11 const map = new Map();
12 for (const item of array) {
13 const key = agg(item);
14 if (!map.has(key)) {
15 map.set(key, item);
16 }
17 }
18 return [...map.values()];
19 }