Created
September 25, 2019 07:38
-
-
Save Sidd27/424659c7df2377bfe172265202141e8e to your computer and use it in GitHub Desktop.
We convert each item into an array that contains the key and the value. mash folds these tuples into an object where they become the actual key/value pairs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.mash = function(callback) { | |
const addKeyValuePair = (acc, item) => { | |
const [key, value] = callback ? callback(item) : item | |
return {...acc, [key]: value} | |
} | |
return this.reduce(addKeyValuePair, {}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment