Skip to content

Instantly share code, notes, and snippets.

@bitifet
Last active January 25, 2024 16:22
Show Gist options
  • Save bitifet/2466e5ce39eef2829fd03990e04fef11 to your computer and use it in GitHub Desktop.
Save bitifet/2466e5ce39eef2829fd03990e04fef11 to your computer and use it in GitHub Desktop.
objectMap(): Kind of Array.map() but for objects...
// USAGE: objectMap(<object>, <callback>)
// <object>: Any regular JavaScript object.
// <callback>: function(<current_value>, <current_key>, <original_object>) {...}
// EXAMPLE:
// objectMap({a: null, b: "23"}, Number) //-> { a: 0, b: 23 }
function objectMap(obj, cbk) {
return Object.fromEntries(
Object.entries(obj).map(
([key, value])=>[key, cbk(value, key, obj)]
)
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment