Skip to content

Instantly share code, notes, and snippets.

@plugn
Last active November 27, 2019 07:42
Show Gist options
  • Save plugn/14f88ff9de3c1b17ad2256cca4708284 to your computer and use it in GitHub Desktop.
Save plugn/14f88ff9de3c1b17ad2256cca4708284 to your computer and use it in GitHub Desktop.
function reduceObject( obj, reduceCallback, initialValue ) {
return Object.keys( obj ).reduce( function( acc, key, idx, arr ) {
return reduceCallback( acc, obj[key], key, obj )
}, initialValue )
}
var mapCollectionByProp = (list, fieldName) => list.reduce( (acc, v) => ({ ...acc, [v[fieldName]]: v }), {})
/**
* returns new object with result of iterator() call for each property from keys
* @param {Object} object
* @param {Array} keys
* @param {Function} iterator
*/
const updateObject = (object, keys, iterator = val => val) =>
keys.reduce((acc, field) => ({ ...acc, [field]: iterator(object[field]) }), {...object})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment