mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
21 lines
626 B
JavaScript
21 lines
626 B
JavaScript
//#region src/array/uniq-by.ts
|
|
/**
|
|
* Returns array with duplicates removed, keeping the LAST occurrence.
|
|
* Useful for feature merging where extensions should override base features.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const features = [{ id: 'a', v: 1 }, { id: 'b', v: 2 }, { id: 'a', v: 3 }];
|
|
* uniqBy(features, s => s.id);
|
|
* // => [{ id: 'b', v: 2 }, { id: 'a', v: 3 }]
|
|
* ```
|
|
*/
|
|
function uniqBy(arr, mapper) {
|
|
const seen = /* @__PURE__ */ new Map();
|
|
arr.forEach((item, i) => seen.set(mapper(item), i));
|
|
return arr.filter((_, i) => [...seen.values()].includes(i));
|
|
}
|
|
//#endregion
|
|
export { uniqBy };
|
|
|
|
//# sourceMappingURL=uniq-by.js.map
|