datasquirel/dist/package-shared/utils/unique-by-key.js
Benjamin Toby 7e8bb37c09 Updates
2025-07-05 14:59:30 +01:00

29 lines
870 B
JavaScript

import slugify from "./slugify";
export default function uniqueByKey(arr, key) {
let newArray = [];
let uniqueValues = [];
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
let targetValue;
if (Array.isArray(key)) {
const targetVals = [];
for (let k = 0; k < key.length; k++) {
const ky = key[k];
const targetValuek = slugify(String(item[ky]));
targetVals.push(targetValuek);
}
targetValue = slugify(targetVals.join(","));
}
else {
targetValue = slugify(String(item[key]));
}
if (!targetValue)
continue;
if (uniqueValues.includes(targetValue))
continue;
newArray.push(item);
uniqueValues.push(targetValue);
}
return newArray;
}