类数组 数组去重

类数组:属性为索引(数字字符串)属性,必须有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())
复制代码

 

posted @   howhy  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· DeepSeek本地性能调优
· 一文掌握DeepSeek本地部署+Page Assist浏览器插件+C#接口调用+局域网访问!全攻略
点击右上角即可分享
微信分享提示