当前位置:网站首页>Difference between Object and Map
Difference between Object and Map
2022-07-30 19:02:00 【Pushed open the door of the world】
Object
- 在ECMAScript中,Object是一个特殊的对象.它本身是一个顶级对象,同时还是一个构造函数,可以通过它(如:new Object())来创建一个对象.我们可以认为JavaScript中所有的对象都是Object的一个实例,对象可以用字面量的方法const obj = {}即可声明.
Map
- Map是Object的一个子类,可以有序保存任意类型的数据,使用键值对去存储,其中键可以存储任意类型,通过const m = new Map();即可得到一个map实例.
访问
map: 通过
map.get(key)
方法去属性, 不存在则返回undefinedobject: 通过
obj.a
或者obj[‘a’]
去访问一个属性, 不存在则返回undefined
赋值
map: 通过map.set去设置一个值,
key可以是任意类型
object: 通过object.a = 1或者object[‘a’] = 1,去赋值,key只能是字符串,数字或symbol
删除
map: 通过map.delete去删除一个值,试图删除一个不存在的属性会返回false
object: 通过delete操作符才能删除对象的一个属性,诡异的是,即使对象不存在该属性,删除也返回true,当然可以通过Reflect.deleteProperty(target, prop) 删除不存在的属性还是会返回true.
var obj = {}; // undefined
delete obj.a // true
大小
map: 通过map.size
即可快速获取到内部元素的总个数
object: 需要通过Object.keys
的转换才能将其转换为数组,再通过数组的length
方法去获得或者使用Reflect.ownKeys(obj)
也可以获取到keys的集合
Reflect.ownKeys 可以获取所有的keys
迭代
map: 拥有迭代器,可以通过for-of forEach去直接迭代元素,且
The traversal order is deterministic
object: 并没有实现迭代器,需要自行实现,不实现只能通过for-in循环去迭代,
遍历顺序是不确定的
Because objects are optimized for properties,排序属性 elements,用来存储数字
. 常规属性 properties 用来存储字符串
.数字属性应该按照索引值⼤⼩升序排列,字符串属性根据创建时的顺序升序排列.并且数字属性优先于字符串.So there will be cases where the traversal order is uncertain.
快属性就是采用线性数据结构
,慢属性就是采用非线性结构
,比如字典来存储数据
使用场景
如果只需要简单的存储key-value
的数据,并且key不需要存储复杂类型的,直接用对象
如果该对象必须通过JSON转换的,则只能用对象,目前暂不支持Map
map的阅读性更好,所有操作都是通过api形式去调用,更有编程体验,比如要make a caching strategy
(() => {
let arr = [1, 2, 3, 1, 2];
const unique = (arr) => {
let map = new Map();
let res = [];
for (let k of arr) {
if (!map.has(k)) {
map.set(k);
res.push(k);
}
}
return res;
};
console.log(unique(arr));
})();
边栏推荐
- VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
- Scala学习:breakable
- 什么是 RESTful API?
- 猎豹移动终于递交年报:年营收7.85亿 腾讯持股16.6%
- ROS 节点初始化步骤、topic/service创建及使用
- Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
- VBA批量将Excel数据导入Access数据库
- What kind of framework is friendly to developers?
- 中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
- 7.30模拟赛总结
猜你喜欢
生物医学论文有何价值 论文中译英怎样翻译效果好
【Pointing to Offer】Pointing to Offer 18. Delete the node of the linked list
nlohmann json 使用指南【visual studio 2022】
【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list
延时队列优化 (2)
Swiper轮播图片并播放背景音乐
深入浅出边缘云 | 3. 资源配置
固定资产可视化智能管理系统
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
【Prometheus】Prometheus联邦的一次优化记录[续]
随机推荐
redis
Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents
OneFlow源码解析:Op、Kernel与解释器
自然语言处理nltk
又一家公司面试的内容
requet.getHeader(“token“) 为null
OneFlow source code analysis: Op, Kernel and interpreter
[TypeScript]编译配置
Multiple instances of mysql
Swiper轮播图片并播放背景音乐
LeetCode 练习——关于查找数组元素之和的两道题
C# wpf borderless window add shadow effect
7.30模拟赛总结
The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!
常见链表题及其 Go 实现
Does the satellite phone communicate directly with the satellite or through a ground station?
防抖和节流有什么区别,分别用于什么场景?
Scrapy框架介绍
The use of terminal split screen tool Terminalx
LeetCode Exercise - Two Questions About Finding Sum of Array Elements