类数组 数组去重
类数组:属性为索引(数字字符串)属性,必须有length属性,最好加上push
var obj = { '1': 'aa', '2': 22, 'length': 2, 'push': Array.prototype.push } // Array.prototype.push = function (item) { // obj[obj.length] = item // obj.length++ // } obj.push('bbb') console.log(obj)//{1: 'aa', 2: 'bbb', length: 3, push: ƒ}
function myTypeOf(obj) { var typeStr = '' var objtype = typeof (obj) if (objtype === 'object' || objtype === 'function') { if (obj === null) { typeStr = 'null' } else { typeStr = Object.prototype.toString.call(obj) if (typeStr === '[object Array]') { typeStr = 'array' } else if (typeStr === '[object Object]' || typeStr === '[object Function]') { typeStr = 'object' } } } else { typeStr = objtype } return typeStr } Array.prototype.unique = function () { var len=this.length // var newArr=[] // for(var i=0;i<len;i++){ // if(newArr.indexOf(this[i])===-1){ // newArr.push(this[i]) // } // } // return newArr var obj = {} for (var i = 0; i < len; i++) { var key = '' + this[i] if (!obj.hasOwnProperty(key)) { obj[key] = 'aa' } } return Object.keys(obj) } var arr = [1, 4, 4, 1, 33, 55, 11, 5, 33] console.log(arr.unique())