map-fns
Version:
<h1 align="center"> <code>map-fns</code> </h1>
31 lines (29 loc) • 1.05 kB
JavaScript
/**
* Returns a new array of key, value entries from the provided map. The keys of the entries
* to return may be provided via the `keys` argument.
*
* ```tsx
* const entries = mapEntries({ a: 1, b: 2, c: 3 });
* // > [["a", 1], ["b", 2] ["c", 3]]
*
* const entries = mapEntries({ a: 1, b: 2, c: 3 }, ["b", "a"]);
* // > [["b", 2], ["a", 1]]
* ```
*
* @param map The map to get the entries of.
* @param keys The keys in the map to return as entries.
* @returns An array of key, value pairs for every entry in the map. If `keys` is provided, only the entries for those keys are included.
*/
function mapEntries(map, keys) {
var keysToUse = keys || Object.keys(map);
var entries = [];
for (var _i = 0, keysToUse_1 = keysToUse; _i < keysToUse_1.length; _i++) {
var key = keysToUse_1[_i];
if (!map.hasOwnProperty(key)) {
throw new Error("Key '".concat(key, "' does not exist in map."));
}
entries.push([key, map[key]]);
}
return entries;
}
export { mapEntries as default };