- //方法一、Set()+Array.from()
- const arr = [1, 2, 2, 'abc', 'abc', true, true, false, false, undefined, undefined, NaN, NaN]
- const result = Array.from(new Set(arr))
- console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ]
- //方法二:Set()+...操作符
- //写法1
- var mySet = new Set([1, 2, 3, 4, 4]);
- [...mySet]; // [1, 2, 3, 4]
-
- //写法2
- //(2)ES6常用:Set去重(去重代码少。但是无法去除{}空对象。)`
- function unique(arr) {
- return Array.from(new Set(arr))
- }
- var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN, 'NaN', 0, 0, 'a', 'a', {}, {}
- ];
- console.log(unique(arr))
-
- //方法三:
- // 根据对象中属性名不能重复 obj = { age: 20, age: 30 } ==> obj={ age: 30 } //前面的会被覆盖
- // 就是创建一个对象,对象里的属性名是数组的元素:使用for遍历 ==》obj[arr[i]] = 随便写一个数
- // 然后用Object.keys() ==> 对象的所有属性名,返回一个数组
-
- var arr = [2, 3, 4, 4, 6, 7, 8, 4, 9, 7]
- console.log(arr)
- var obj = {}
- for (var i = 0; i < arr.length; i++) {
- obj[arr[i]] = "11"
- }
- var re = Object.keys(obj)
- console.log(re)
- var arr2 = []
- for (var j = 0; j < re.length; j++) {
- arr2.push(parseInt(re[j]))
- }
- console.log(arr2)
- //方法四:利用两层循环+数组的splice方法 NaN和{}没有去重
- const arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN, 'NaN', 0, 0, 'a', 'a', {}, {}];
- function removeDuplicate(arr) {
- let len = arr.length
- for (let i = 0; i < len; i++) {
-
- for (let j = i + 1; j < len; j++) {
- if (arr[i] === arr[j]) {
- arr.splice(j, 1)
- len-- // 减少循环次数提高性能
- j-- // 保证j的值自加后不变
- }
- }
- }
- return arr
- }
- var result = removeDuplicate(arr)
- console.log(result) //14) [1, 'true', true, 15, false, undefined, null, NaN, NaN, 'NaN', 0, 'a', {…}, {…}]
- //方法五:利用数组的indexOf方法
- //写法1
- const arr = [1, 2, 2, 'abc', 'abc', true, true, false, false, undefined, undefined, NaN, NaN]
- function removeDuplicate(arr) {
- const newArr = []
- arr.forEach(item => {
- if (newArr.indexOf(item) === -1) {
- newArr.push(item)
- }
- })
- return newArr // 返回一个新数组
- }
- const result = removeDuplicate(arr)
- console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN, NaN ]
-
-
- //写法2
- //indexOf去重(NaN、{}没有去重)`
- function unique(arr) {
- if (!Array.isArray(arr)) {
- console.log('type error!')
- return
- }
- var array = [];//新建空数组,for循环原数组,判断数组是否存在当前元素,如果相同的值跳过,不同push进数组
- for (var i = 0; i < arr.length; i++) {
- if (array.indexOf(arr[i]) === -1) {
- array.push(arr[i])
- }
- }
- return array;
- }
- var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN, 'NaN', 0, 0, 'a', 'a', {}, {}
- ];
- console.log(unique(arr)) //(14) [1, 'true', true, 15, false, undefined, null, NaN, NaN, 'NaN', 0, 'a', {…}, {…}]
- //方法六:利用数组的includes方法
- const arr = [1, 2, 2, 'abc', 'abc', true, true, false, false, undefined, undefined, NaN, NaN]
- function removeDuplicate(arr) {
- const newArr = []
- arr.forEach(item => {
- if (!newArr.includes(item)) {
- newArr.push(item)
- }
- })
- return newArr
- }
- const result = removeDuplicate(arr)
- console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ]
-
- //方法七:利用数组的filter()+indexOf()
- //写法1
- const arr = [1, 2, 2, 'abc', 'abc', true, true, false, false, undefined, undefined, NaN, NaN]
- function removeDuplicate(arr) {
- return arr.filter((item, index) => {
- return arr.indexOf(item) === index
- })
- }
- const result = removeDuplicate(arr)
- console.log(result) // [ 1, 2, 'abc', true, false, undefined ]
-
- //写法2:
- //filter({}没有去重、NaN两个都没有了)`
- function unique(arr) {
- return arr.filter(function (item, index, arr) {
- //当前元素,在原始数组中的第一个索引==当前索引值,否则返回当前元素
- return arr.indexOf(item, 0) === index;
- });
- }
- var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,
- 'NaN', 0, 0, 'a', 'a', {}, {}
- ];
- console.log(unique(arr)) //(12) [1, 'true', true, 15, false, undefined, null, 'NaN', 0, 'a', {}}, {}]
- //方法八:利用Map()
- const arr = [1, 2, 2, 'abc', 'abc', true, true, false, false, undefined, undefined, NaN, NaN]
- function removeDuplicate(arr) {
- const map = new Map()
- const newArr = []
- arr.forEach(item => {
- if (!map.has(item)) { // has()用于判断map是否包为item的属性值
- map.set(item, true) // 使用set()将item设置到map中,并设置其属性值为true
- newArr.push(item)
- }
- })
- return newArr
- }
- const result = removeDuplicate(arr)
- console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ]
- //方法九:利用对象
- const arr = [1, 2, 2, 'abc', 'abc', true, true, false, false, undefined, undefined, NaN, NaN]
- function removeDuplicate(arr) {
- const newArr = []
- const obj = {}
- arr.forEach(item => {
- if (!obj[item]) {
- newArr.push(item)
- obj[item] = true
- }
- })
- return newArr
- }
- const result = removeDuplicate(arr)
- console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ]
- //方法十:sort()排序` 排序后的结果,遍历对比
- function unique(arr) {
- if (!Array.isArray(arr)) {
- console.log('type error!')
- return
- }
- arr = arr.sort()
- var arrray = [arr[0]];
- for (var i = 0; i < arr.length; i++) {
- if (arr[i] !== arr[i - 1]) {
- arrray.push(arr[i]);
- }
- }
- return arrray
- }
- var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,
- 'NaN', 0, 0, 'a', 'a', {}, {}
- ];
- console.log(unique(arr))//(15) [0, 0, 1, 15, NaN, NaN, 'NaN', {…}, {…}, 'a', false, null, 'true', true, undefined]
- //方法十一:hasOwnProperty (所有都去重) `
- var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,
- 'NaN', 0, 0, 'a', 'a', {}, {}
- ];
- function unique(arr) {
- var obj = {};
- return arr.filter(function (item, index, arr) {
- return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)
- })
- }
- console.log(unique(arr)) //(12) [1, 'true', true, 15, false, undefined, null, NaN, 'NaN', 0, 'a', {}}]