Home > Article > Web Front-end > Three simple solutions to deduplicate js arrays
1. es6 Set to remove duplicates
function removal(arr) { return Array.from(new Set(arr)) } let arr=[1,2,1,3,4,5,5] removal(arr)//[1, 2, 3, 4]
2. Use filter
[Related course recommendations: JavaScript video tutorial]
function removal(arr){ return arr.filter((item,index,arr)=>{ return arr.indexOf(item,0) == index; }) } let arr=[1,2,1,3,4,5,5] removal(arr)//[1, 2, 3, 4]
3. Using reduce
let newArr = arr.reduce((prev, cur) => { prev.indexOf(cur) === -1 && prev.push(cur); return prev; },[]);
This article comes from js tutorial column, welcome to learn!
The above is the detailed content of Three simple solutions to deduplicate js arrays. For more information, please follow other related articles on the PHP Chinese website!