方式一
Array.from(new Set(arr)
var arr = [3, 77, 7,1, 2, 2, 3, 8, 0];console.log(Array.from(new Set(arr)))
方式二
判断是原始值 对象 null
对于null类型的数据,直接通过===判断
对于原始子我们使用typeof判断function 原始类型 object
对于object我们需要tostring方法进行判断
例如
function type(target) {var template={"[object Array]":"array","[object Object]":"object","[object String]":"string -object","[object Boolean]":"boolean -object","[object Number]":"number -object",}if (target === null) {return "null"}if(typeof(target) ==="object"){var str = Object.prototype.toString.call(target)return template[str]}else{return typeof(target)}}type(null)
数组去重
原理:对象的属性不允许重复,我们可以把数组遍历作为对象的属性,然后给属性赋值,然后判断对象是不是undefine
对象[数组值]是否为undefine
var arr =[1,1,1,2,3,3,4,5,5]Array.prototype.unique=function(){var temp={},arr=[],len = this.length;for(var i=0;i<len;i++){if(!temp[this[i]]){temp[this[i]]="abc"arr.push(this[i])}}return arr}
