Map.keys() returns a MapIterator object which can be converted to Array using Array.from:
let keys = Array.from( myMap.keys() );
// ["a", "b"]
EDIT: you can also convert iterable object to array using spread syntax
let keys =[ ...myMap.keys() ];
// ["a", "b"]
Answer from pawel on Stack OverflowVideos
Map.keys() returns a MapIterator object which can be converted to Array using Array.from:
let keys = Array.from( myMap.keys() );
// ["a", "b"]
EDIT: you can also convert iterable object to array using spread syntax
let keys =[ ...myMap.keys() ];
// ["a", "b"]
You can use the spread operator to convert Map.keys() iterator in an Array.
let myMap = new Map().set('a', 1).set('b', 2).set(983, true)
let keys = [...myMap.keys()]
console.log(keys)
This is not a Map object. It's just a regular object. So, use Object.entries and then use map on the key value pair:
const map = {"a": 1, "b": 2, "c": 3};
const mapped = Object.entries(map).map(([k,v]) => `${k}_${v}`);
console.log(mapped);
Object.entries returns:
[["a",1],["b",2],["c",3]]
Then loop through each of those inner arrays and create the string using template literals
If you have a Map object, use Array.from(map) to get the entries of the map and use the second parameter of Array.from to go over each entry and create the desired string
Array.from(map, ([k,v]) => `${k}_${v}`)
It's not a map, it's an object. (You might consider using a Map, though.)
To get its properties as key/value pairs, you can use Object.entries, which you can then apply map to:
map = Object.entries(map).map(([key, value]) => key + "_" + value);
Object.entries is relatively new, but easily polyfilled for older environments.
Live Example:
var map = {"a": 1, "b": 2, "c": 3};
map = Object.entries(map).map(([key, value]) => key + "_" + value);
console.log(map);
Or, using a Map, you can use its built-in entries method, which returns an iterable, passing it into Array.from and using Array.from's mapping callback:
var map = new Map([
["a", 1],
["b", 2],
["c", 3]
]);
map = Array.from(map.entries(), ([key, value]) => key + "_" + value);
console.log(map);
(Or expand the iterable into an array — [...map.entries()] — and use map on it, but the above avoids a temporary throw-away array.)
In both cases, I'm using destructuring in the parameter list of the arrow function, which receives an array in [key, value] format.
Map.keys() returns an iterator you can spread the iterator using spread syntax
const map = new Map();
map.set(0, 'Zero');
map.set(1, 'One');
map.set(2, 'Two');
[...map.keys()].forEach(key => {
console.log(key);
})
It's Array.prototype.map actually, it's defined for arrays, so use Array.from to convert the keys to an array and then use map:
const map = new Map();
map.set(0, 'Zero');
map.set(1, 'One');
map.set(2, 'Two');
console.log(...Array.from(map.keys()).map(key => {
return key ** 2; // square the keys
}));
There is one more which seems to be the fastest:
for (let [key, value] of map.entries()) {
sum+=value
}
JSBench with the methods as per the following. These are the results from fastest to slowest:
Map.prototype.forEach()for ofloop throughMap.prototype.keys()for ofloop throughMap.prototype[Symbol.iterator]()- Create array from
Map.prototype.keys()and thenArray.prototype.forEach()
You can use a for..of loop to loop directly over the map.entries and get the keys.
function getByValue(map, searchValue) {
for (let [key, value] of map.entries()) {
if (value === searchValue)
return key;
}
}
let people = new Map();
people.set('1', 'jhon');
people.set('2', 'jasmein');
people.set('3', 'abdo');
console.log(getByValue(people, 'jhon'))
console.log(getByValue(people, 'abdo'))
You could convert it to an array of entries (using [...people.entries()]) and search for it within that array.
let people = new Map();
people.set('1', 'jhon');
people.set('2', 'jasmein');
people.set('3', 'abdo');
let jhonKeys = [...people.entries()]
.filter(({ 1: v }) => v === 'jhon')
.map(([k]) => k);
console.log(jhonKeys); // if empty, no key found otherwise all found keys.