-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Javascript中的Map类 #155
Comments
Map基本概念
var map = new Map();
map.set(undefined, undefined);
map.set(null, null);
map.set(true, true);
map.set(24, 24);
map.set(BigInt(24), BigInt(24));
map.set('string', 'string');
map.set(Symbol('foo'),Symbol('foo') );
map.set(()=>'function', ()=>'function');
map.set({},{});
map.set([],[]); 除Symbol在控制台的输出比较特殊以外,其余都蛮正常。
symbol作为一个基础数据类型,只能用在对象的key中。我个人是很少用到这个类型,或许在Map类中会有不一样的火花。 问题一:js中对象的key可以是对象吗?若不能,js对象的key只能是什么,value呢? |
普通Object和Map的对比对于Object和Map来说,设置key-value,查询value,删除key,检测key。由于最初js中只有Object一种可以存储key-value的数据类型,所以几乎所有的key-value都是这样存的,所以Map类型也只能用Object将就。但是,有以下几个强烈推荐使用Map数据类型的场景:
|
Map实战替代原始的对象key,value映射
export default [
['业务员', 'salesman'],
['团队', 'team'],
['业务类型', 'businessType'],
];
const dataMap = new Map(data);
dataMap.get('业务员'') |
Map是一个可以体现出javascript不将就态度的数据类型,很多场景下Map比Object更加合适也更加严谨。
学习资料:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
The text was updated successfully, but these errors were encountered: