var array = [1, 2, 1, 1, '1']; function unique(array) { return Array.from(new Set(array)); } console.log(unique(array)); // [1, 2, "1"]
甚至能夠再簡化下:spa
function unique(array) { return [...new Set(array)]; }
還能夠再簡化下:string
var unique = (a) => [...new Set(a)]
此外,若是用 Map 的話:it
function unique (arr) { const seen = new Map() return arr.filter((a) => !seen.has(a) && seen.set(a, 1)) }