DEV Community

Discussion on: JavaScript: How to Remove Duplicate Values from Arrays

Collapse
 
caoshouse profile image
caoshouse • Edited

let originalArray = [1, 2, 3, 4, 1, 2, 3, 4];
for(var i=0,obj={};i<originalArray.length;i++){
obj[originalArray[i]]=null
}
let noDuplicates=Object.keys(obj)

Collapse
 
4umfreak profile image
Mark Voorberg • Edited

Same using .reduce()...

let myArray = [1,2,2,"hello world",4,5,5, {moo: "cow"}];
let myObject = myArray.reduce((acc, x) => {acc[x]=0; return acc}, {});
let noDuplicates = Object.keys(myObject);