+ 1
How to display a key in hashmap in Javascript?
function q1( map = new Map) { var retVal = new Array(map.size); return retVal=map; } console.log(q1([{'a': 1, 'chased': 6}, {'dog': 3, 'an': 2, 'elephant': 8}, {'small': 5}] ));
1 Odpowiedź
+ 3
These two functions show a couple different ways to get an Array of keys from the map.
function convertMapToKeys(map) {
return Array.from(map.keys());
}
function convertMapToKeys2(map) {
return [...map.keys()];
}
The keys method will give you an iterator if you just needed to use it in a loop.
I'm not sure what your q1 function is trying to do, though. You're passing an Array as an actual parameter to a function that defaults to a Map. That's weird. Your Array is structured as if you want to merge key-value pairs from every object in the Array. At least it shows that you mean ES6 Map instead of how the usual JavaScript object can be treated like a hash map.