Created
December 19, 2017 22:01
-
-
Save Made-of-Clay/63e7a5d2c0a2f97166661ae69c2c4cf0 to your computer and use it in GitHub Desktop.
Recreating PHP's array_combine function; generates a map/object literal from two arrays
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
/** | |
* This function mimics PHP's array_combine function | |
* @param {array} keys Array to be used as new object's keys | |
* @param {array} values Array to be mapped to respective keys | |
* @example | |
* array_combine(['Rick', 'Morty'], ['Sanchez', 'Smith']); // returns { Rick:'Smith', Morty:'Smith' } | |
*/ | |
function array_combine(keys, values) { | |
let obj = {}; | |
keys.forEach((k, i) => obj[k] = values[i]); | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment