Last active
November 15, 2022 17:13
-
-
Save telekosmos/3b62a31a5c43f40849bb to your computer and use it in GitHub Desktop.
Remove duplicates from js array (ES5/ES6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var uniqueArray = function(arrArg) { | |
return arrArg.filter(function(elem, pos,arr) { | |
return arr.indexOf(elem) == pos; | |
}); | |
}; | |
var uniqEs6 = (arrArg) => { | |
return arrArg.filter((elem, pos, arr) => { | |
return arr.indexOf(elem) == pos; | |
}); | |
} | |
var test = ['mike','james','james','alex']; | |
var testBis = ['alex', 'yuri', 'jabari']; | |
console.log(uniqueArray(test.concat(testBis))); |
let arr = [1, 2, 3, 4, 3, 2];
arr.filter((e, i, arr) => arr.indexOf(e) == i);
let arr = [{x:10, y: 20}, {x:10, y: 30}, {x:10, y: 20}]; // each element has x and y props
arr.filter((e, i, arr) => arr.findIndex(e2 => Object.keys(e2).every(prop => e2[prop] == e[prop])) == i);
I find this reads a little bit better if you stash your reducer function in a named variable.
const duplicates = (e, i, arr) => arr.indexOf(e) === i
let arr = [1, 2, 3, 4, 3, 2];
arr.filter(duplicates);
Array.prototype.uniq = function(key) {
return key
? this.map(e => e[key])
.map((e, i, final) => final.indexOf(e) === i && i)
.filter(e => this[e])
.map(e => this[e])
: [...new Set(this)];
}
In Typescript
export const dedupeByProperty =
<T>(arr: ReadonlyArray<T>, objKey: keyof T) =>
arr.reduce<ReadonlyArray<T>>((acc, curr) =>
acc.some(a => a[objKey] === curr[objKey])
? acc
: [...acc, curr], [])
Find brief article here : Click here to view
const uniq = elements.reduce((acc, value) => acc.some(i => i.id === value.id) ? acc : acc.concat(value), []); // id your uniq key
Excelent, thanks!
const deduplicateArrays = ( arr1, arr2 = [] ) => [
...new Set([
...arr1.map(i => JSON.stringify(i)),
...arr2.map(i => JSON.stringify(i))
])
].map(i => JSON.parse(i))
Hi @gevera
Beware of problems when using JSON.stringify and JSON.parse with:
- number: https://github.com/josdejong/lossless-json
- date: https://stackoverflow.com/questions/31096130/how-to-json-stringify-a-javascript-date-and-preserve-timezone
Being bitten more than once... 🤕
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's wrong with
let arr1 = ["apples", "apples", "oranges", "bananas"]; arr1 = Array.from(new Set(arr1));
Surely this is a lot simpler if you're just trying to remove duplicates. Obviously this would work better as the return statement in a function. I'm just posting this as an example.