Is it possible to have multiple keys giving the same result in a JS Map, without having to write all of them ?
For example:
const mp = new Map<number, string>([
[2 || 4, 'even'],
[1 || 3, 'odd']
]);
This does not work but I am looking for something similar to avoid a switch case which is very verbose
CodePudding user response:
Is it possible to have multiple keys giving the same result in a JS Map, without having to write all of them ?
No. A map is a 1:1 connection between a key and a value. If you want two different keys to have the same value, that means you need to create two entries in the map.
CodePudding user response:
Yes, it is possible. You just have to set the value twice for each key. The constraint to be unique is applicable only on keys.
const map = new Map()
map.set(1, 'odd');
map.set(3, 'odd');
