Skip to content

Instantly share code, notes, and snippets.

@telekosmos
Last active November 15, 2022 17:13
Show Gist options
  • Save telekosmos/3b62a31a5c43f40849bb to your computer and use it in GitHub Desktop.
Save telekosmos/3b62a31a5c43f40849bb to your computer and use it in GitHub Desktop.
Remove duplicates from js array (ES5/ES6)
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)));
@Kr3m
Copy link

Kr3m commented Feb 8, 2019

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.

@little-brother
Copy link

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);

@philihp
Copy link

philihp commented Feb 26, 2019

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);

@indatawetrust
Copy link

indatawetrust commented Mar 15, 2019

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)];
}

@patrickmichalina
Copy link

patrickmichalina commented Mar 16, 2019

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], [])

@pankajkrr
Copy link

Find brief article here : Click here to view

@fosteev
Copy link

fosteev commented Mar 10, 2020

const uniq = elements.reduce((acc, value) => acc.some(i => i.id === value.id) ? acc : acc.concat(value), []); // id your uniq key

@ioness
Copy link

ioness commented Apr 27, 2020

Excelent, thanks!

@gevera
Copy link

gevera commented Nov 15, 2022

const deduplicateArrays = ( arr1, arr2 = [] ) => [
...new Set([
           ...arr1.map(i => JSON.stringify(i)),
           ...arr2.map(i => JSON.stringify(i))
          ])
].map(i => JSON.parse(i))

@guillaumegarcia13
Copy link

Hi @gevera
Beware of problems when using JSON.stringify and JSON.parse with:

Being bitten more than once... 🤕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment