Skip to content

Instantly share code, notes, and snippets.

@willywongi
Created December 29, 2011 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willywongi/1534226 to your computer and use it in GitHub Desktop.
Save willywongi/1534226 to your computer and use it in GitHub Desktop.
Return a new object from an array of key/value pairs
/**
* Return a new object from an array of key/value pairs.
* Inspired by the "dict" constructor in python.
*
* @method dict
* @param {Array} arr An array of key/value pairs.
* @return {Object} A new object
* @static
*/
Y.namespace('Object').dict = function(arr) {
for (var o = {}, i = 0, l = arr.length; i<l; i++) {
o[arr[i][0]] = arr[i][1];
}
return o;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment