We will look into a quick recipe for converting a javascript map into an array. This can be pretty useful when you are trying to store the values or keys of the map without worrying about the relationship between the key and value
A map is a dictionary of key and value pairs. You can use the map.keys()
method to get a list of keys from the map. Then we can convert it into an array using either the Array.from
constructor method or use the spread operator
let userData = new Map();
userData.set('name', 'Test');
userData.set('age', '24');
userData.set('hobby', 'Blogging')
// Options 1 - Using the Array.from syntax
const userDataKeys = Array.from(userData.keys());
console.log(userDataKeys);
// Options 2 - Using spread operator
const userDataKeysSpread = [...userData.keys()];
console.log(userDataKeysSpread);
Let’s breakdown the steps in the above example for Option 1
Map
variable called userData
[variable].keys()
methodArray.from()
syntaxSteps for Options 2
...
to convert the return value of the [variable].keys()
method to arrayOutput:
["name", "age", "hobby"]
["name", "age", "hobby"]
You can use the map.values()
method to get a list of keys from the map. Then we can convert it into an array using either the Array.from
constructor method or use the spread operator
let userData = new Map();
userData.set('name', 'Test');
userData.set('age', '24');
userData.set('hobby', 'Blogging')
// Options 1 - Using the Array.from syntax
const userDataValues = Array.from(userData.values());
console.log(userDataValues);
// Options 2 - Using spread operator
const userDataValuesSpread = [...userData.values()];
console.log(userDataValuesSpread);
Let’s breakdown the steps in the above example for Option 1
Map
variable called userData
[variable].values()
methodArray.from()
syntaxSteps for Options 2
...
to convert the return value of the [variable].values()
method to arrayOutput:
["Test", "24", "Blogging"]
["Test", "24", "Blogging"]
Let’s look at a quick example of how you can accomplish the same thing using Typescript
let userData = new Map<string, string>();
userData.set('name', 'Test');
userData.set('age', '24');
userData.set('hobby', 'Blogging')
// Options 1 - Using the Array.from syntax
const userDataValues: string[] = Array.from(userData.values());
console.log(userDataValues);
// Options 2 - Using spread operator
const userDataValuesSpread: string[] = [...userData.values()];
console.log(userDataValuesSpread);
Since we are storing both the key and value as string data types, we can define the userData
map variable with Map<string, string>
to represent that both key and value are string types.
userData.values()
method will return the value of type IterableIterator<string>
which can be passed to the Array.from
or spread operator to convert that into a string[]
type.
If you want to store the key as a number, you can do the following
let userData2 = new Map<number, string>();
userData2.set(1, "Test");
const userDataKeys: number[] = [...userData2.keys()]
Please feel free to reach out to us if you have a better solution or feedback on the post.
If you want to learn more about web technologies, you can check out our Discord server - https://discord.gg/AUjrcK6eep
Sriram is a content creator focused on providing quality content which provides value for the readers. He believe is constant learning and sharing those learning with the group. He is working as a lead developer and bulk of his experience is in working with multiple javascript frameworks such as Angular, React, Svelte. He is passionate about open source technologies and on his free time loves to develop games.
We will reach out when exciting new posts are available. We won’t send you spam. Unsubscribe at any time.