Skip to content

Instantly share code, notes, and snippets.

@Made-of-Clay
Created December 19, 2017 22:01
Show Gist options
  • Save Made-of-Clay/63e7a5d2c0a2f97166661ae69c2c4cf0 to your computer and use it in GitHub Desktop.
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 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