const { random } = Math,
randomStr = (minLen, maxLen) =>
Array
.from(
{length: random()*(maxLen-minLen+1)+minLen},
() => String.fromCharCode(random()*26+97)
)
.join(''),
uniqueArray = Array
.from(
{length: 100},
() => ({
key: randomStr(3,7),
value: randomStr(2,8)
})
),
sourceArray = Array(1000)
.fill()
.map(() => uniqueArray[0|random()*uniqueArray.length])
const dedupe = (arr, keyProp) => [
...arr
.reduce((acc, obj) =>
(acc.set(obj[keyProp], obj), acc), new Map)
.values()
]
dedupe(sourceArray, 'key')
const dedupe = (arr, keyProp) => Array.from(new Set(arr.map((a) => a[keyProp]))).map(
(Numbers) => {
return arr.find((a) => a[keyProp] === Numbers);
}
)
dedupe(sourceArray, 'key')
const dedupe = (arr, keyProp) => Object.values(Object.fromEntries(arr.map(a => [a[keyProp], a])));
dedupe(sourceArray, 'key')
const dedupe = ([...arr], keyProp) => {
const numbers = new Set();
for (let i = arr.length - 1; i >= 0 ; i--) {
const { [keyProp]: number } = arr[i];
if (numbers.has(number)) arr.splice(i, 1);
numbers.add(number);
}
return arr
}
dedupe(sourceArray, 'key')