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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
}

Tip: We can also create arrays for chargeLevelMap.keys() and chargeLevelMap.values().