非基本类型属性或者对象进行判断

https://caniuse.com/
@babel/plugin-propsal-optional-chaining 可选链
@babel/plugin-propsal-nullish-coalescing-operator 双问号

??

  let  res = 0
  let o = res??{}
  console.log(o)

  let res = {data:1}
  let o = res.data?? ' '
  console.log(res)

?.

  let res = {data:info:[{obj:{id:1}}]}
  let id = res.data.info[0].obj.id
  let id1 =  res?.data?.info[0]?.obj?.id

??=

  let b = '你好';
  let a = 0
  let c = null;
  let d = ’123‘
  b ??= a;  // b = “你好”
  c ??= d  // c = '123'

set 方法 去重

  var arr = [1,1,1,2]
  let b = new Set(arr)
  console.log(b)
  Set(2) {1, 2}
  let newArr = Array.from(b)
  console.log(newArr)
   (2) [1, 2]
  Array.from(new Set(arr))  //整体写法
  [...new Set(arr)]               //整体写法

map方法

  let o = new Map()
  let obj = {}
  map.set(1,'a')
  map.set('2','a')
  map.set(obj,'a')
  for(let v of o){
     console.log('值为:',v)
  }

三种迭代对象 全部entries key value

  for(let v of o.entries()){
     console.log('值为:',v)
  }
  for(let v of o.key()){
     console.log('key为:',v)
  }
  for(let v of o.value()){
     console.log('value为:',v)
  }

二维数组

  let m = new Map([[1,'a'],[2,'b']])
  console.log(m.has(1))
  m.set(3,'c').set(4,'d')
  let arr = Array.from(m)
  console.log(arr)
  相同的key 会覆盖
  m.set(3,'c')

手写 map 函数

 Array.prototype.Map= function (callback, thisArg) {
    let length = this.length
    let res = []
    if (!Array.isArray(this)) throw new TypeError('this is not an array')
    if (typeof callback !== 'function') throw new TypeError(callback + 'is not a function')
    if (length === 0) {
        return res
    }
    for (let i = 0; i < length; i++) {
      res[i] = callback.call(thisArg, this[i], i, this)
    }
    return res
  }
  let a = [{a:1},{b:2}]
  let b = []
  b = a.Map(item=>{return item})
  console.log(b)

手写 new Map()函数

  class Map{
    // Symbol.iterator会返回一个对象,这就是一个遍历器对象,而作为遍历器对象,其必须具备的特征就是必须具备next()方法。
    constructor(iterable = []){
         //判断是否是可迭代对象
         if(typeof iterable[Symbol.iterator] !== "function"){
            throw new Error(`你提供的${iterable}不是一个课迭代的对象`);
        }
        this._datas = [];
        for (const item of iterable) {
            //item也是一个可迭代的对象
            if(typeof item[Symbol.iterator] !== "function"){
                throw new Error(`你提供的${item}不是一个课迭代的对象`)
            }
            const iterator = item[Symbol.iterator]();
            const key = iterator.next().value;
            const value = iterator.next().value;
            this.set(key,value);
        }

    }
    set(key,value){
        //看里面有没有,如果有,则直接修改key对应的value值
        const obj = this._getObj(key);
        if(obj){
            //修改
            obj.value = value;
        }else{
            this._datas.push({
                key,
                value
            })
        }

    }
    get(key){
        const item = this._getObj(key);
        if(item){
            return item.value;
        }
        return undefined;
    }
    get size(){
        return this._datas.length;
    }
    delete(key){
       for (let i = 0; i < this._datas.length; i++) {
           const element = this._datas[i];
           if(this.isEqual(element.key,key)){
               this._datas.splice(i,1);
               return true;
           }
       }
       return false;
    }
    clear(){
        this._datas.length = 0;
    }
    has(key){
        const item = this._getObj(key);
        return item !== undefined;
    }
    /**
     * 根据key值找到对应的数组项
     * @param {*} key 
     */
    _getObj(key){
        for (const item of this._datas) {
            if(this.isEqual(item.key,key)){
                return item;
            }
        }
        return undefined;
    }
    isEqual(data1,data2){
        if(data1 === 0 && data2 === 0){
            return true;
        }
        return Object.is(data1,data2);
    }
    *[Symbol.iterator](){
        for (const item of this._datas) {
            yield[item.key,item.value];
        }
    }
    forEach(callback){
        for (const item of this._datas) {
            callback(item.value,item.key,this);
        }
    }
}

推荐阅读更多精彩内容