map/set/object/array对比

2018/03/06 18:41
阅读数 27
map () {
      //数据结构横向对比, 增,查,改,删
      let map = new Map()
      let array = []
      //增
      map.set('t',1)
      array.push({t:1})

      console.info('map-array',map,array)

      //查
      let map_exist = map.has('t')
      let array_exist = array.find(item => item.t)
      console.info('map-array',map_exist,array_exist)

      //改
      map.set('t',2)
      array.forEach(item => item.t?item.t=2:'')
      console.info('map-array-modefy',map,array)

      //删
      map.delete('t')
      let index = array.findIndex(item=>item.t)
      array.splice(index,1)
      console.info('map-array',map,array)
    },

    set () {
      let set = new Set()
      let array = []

      //增
      let obj = {t:1}
      set.add(obj)
      array.push(obj)
      console.log('set-array',set,array)

      //查
      let set_exist = set.has(obj)
      let array_exist = array.find(item=>item.t)
      console.info('set-array',set_exist,array_exist)

      //改
      set.forEach(item => item.t?item.t=2:'')
      array.forEach(item => item.t?item.t=2:'')
      console.log('set-array',set,array)

      //删
      set.forEach(item => item.t?set.delete(item):'')
      let index = array.findIndex(item=>item.t)
      array.splice(index,1)
      console.info('set-array',set,array)
    },

    test () {
      //map set object 对比
      let item = {t:1}
      let map = new Map()
      let set = new Set()
      let obj = {}

      //增
      map.set('t',1)
      set.add(item)
      obj['t'] = 1
      console.log('map-set-obj',map,set,obj)

      //查
      console.log({
        map_exist: map.has('t'),
        set_exist: set.has(item),
        obj_exist: 't' in obj
      })

      //改
      map.set('t',2)
      item.t = 2
      obj['t'] = 2
      console.log('map-set-obj',map,set,obj)

      //删
      map.delete('t')
      set.delete(item)
      delete obj['t']
      console.log('map-set-obj',map,set,obj)
    }
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部