欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

map/set/object/array对比

发布时间:2025/6/17 编程问答 66 豆豆
生活随笔 收集整理的这篇文章主要介绍了 map/set/object/array对比 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
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'] = 1console.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 = 2obj['t'] = 2console.log('map-set-obj',map,set,obj)//删map.delete('t')set.delete(item)delete obj['t']console.log('map-set-obj',map,set,obj)}

转载于:https://www.cnblogs.com/yangAL/p/8516108.html

总结

以上是生活随笔为你收集整理的map/set/object/array对比的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。