How to convert an iterable to array in JavaScript
An Iterable is an object where you can iterate its keys.
Sometime we might want to convert those as array of values so that we can process or transform into something else.
js
const chargeLevelMap = new Map();chargeLevelMap.set('Critical', 10);chargeLevelMap.set('Half', 50);chargeLevelMap.set('Full', 100);function determineChargeLevel(charge) {// ["Critical", 10], ["Half", 50], ["Full", 100]const chargeLevelEntries = Array.from(chargeLevelMap.entries());const matchedEntry = chargeLevelEntries.find((kv) => charge <= kv[1]);return matchedEntry[0];}// With some destructuring...function determineChargeLevel(charge) {const chargeLevelEntries = Array.from(chargeLevelMap.entries());const [level, charge] = chargeLevelEntries.find(([key, value]) => charge <= value);return level;}
We can also create arrays for
chargeLevelMap.keys()
andchargeLevelMap.values()
.