admin管理员组文章数量:1300184
I have a ES6 Map , where keys are number. Some time the key is number and some time the key is string which represents a number. The map will never have duplicate key at runtime for me. Example I will never have key "1" and 1 .
While retrieving from map I need a simple one liner code which will negate whether if the key is a string or a number.
var map = new Map();
undefined
map.set('1', 'string one');
map.set(2, 'number tow')
Map(2) {"1" => "string one", 2 => "number tow"}
map.get(1)
undefined
map.get('1')
"string one"
I have a ES6 Map , where keys are number. Some time the key is number and some time the key is string which represents a number. The map will never have duplicate key at runtime for me. Example I will never have key "1" and 1 .
While retrieving from map I need a simple one liner code which will negate whether if the key is a string or a number.
var map = new Map();
undefined
map.set('1', 'string one');
map.set(2, 'number tow')
Map(2) {"1" => "string one", 2 => "number tow"}
map.get(1)
undefined
map.get('1')
"string one"
Share
asked Nov 14, 2017 at 12:11
mbarish-membarish-me
9571 gold badge14 silver badges26 bronze badges
4
- 4 please add a question. – Nina Scholz Commented Nov 14, 2017 at 12:12
- 4 You'd better rethink your appoach and stick with either numbers or strings. – Yury Tarabanko Commented Nov 14, 2017 at 12:14
- 6 why not use an object? all keys are converted to strings. – Nina Scholz Commented Nov 14, 2017 at 12:21
-
I agree with @NinaScholz there's no point in using a
Map
if you're never gonna have both1
and'1'
... – anteAdamovic Commented Nov 14, 2017 at 12:36
3 Answers
Reset to default 6You could use just an object with no prototypes. For the access the key is converted to string.
var map = Object.create(null);
map['1'] = 'string one';
map[2] = 'number two';
console.log(map[1]); // 'string one'
console.log(map['1']); // 'string one'
console.log(map);
For starters, I would remend that you will sanitize your keys (e.g use only strings or only numbers) and by this you will make your life easier.
If you still insist on using both type of keys you could create a wrapper function like this :
function getFromMap(map, key){
var stringKey = key.toString();
var integerKey = parseInt(key);
return map.get(stringKey) || map.get(integerKey);
}
As a side note : It seems that in your case you could easily use an ordinary object (Using brackets for object assignment will automatically convert the numbers to strings) :
var o = {};
o[1] = 'First value';
console.log(o[1] === o['1']); // true
For furthor reading about the advantages/disadvantages of using Map vs Objects you are invited to read the following SA question .
I've created a function that gets the value from the given map. It works both ways, from string to int and from int to string.
var map = new Map();
map.set('1', 'string one');
map.set(2, 'number two');
function getFromMap(map, key){
return map.get(key) || map.get(key.toString()) || map.get(parseInt(key));
}
console.log(getFromMap(map, 1));
console.log(getFromMap(map, '2'));
本文标签: javascriptES6 Map key to be either string or numberStack Overflow
版权声明:本文标题:javascript - ES6 Map key to be either string or number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741659849a2390969.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论