类数组 数组去重
类数组:属性为索引(数字字符串)属性,必须有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())
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· DeepSeek本地性能调优
· 一文掌握DeepSeek本地部署+Page Assist浏览器插件+C#接口调用+局域网访问!全攻略