Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created April 13, 2011 23:07
Show Gist options
  • Save jboesch/918620 to your computer and use it in GitHub Desktop.
Save jboesch/918620 to your computer and use it in GitHub Desktop.
Backwards compat for versions of jQuery prior to 1.6 that want to use objects in jQuery.map
// Only load this in if the version of jQuery is prior to 1.6
if(parseFloat(jQuery.fn.jquery) < 1.6){
jQuery.map = (function(_oldmap){
return function(){
var elems = arguments[0],
length = elems.length,
isArray = elems instanceof jQuery || length !== undefined && typeof length === "number" && ((length > 0 && elems[0] && elems[length -1]) || jQuery.isArray(elems));
if(!isArray){
var ret = [];
for ( key in elems ) {
value = arguments[1](elems[key], key, arguments[2]);
if (value != null) {
ret[ret.length] = value;
}
}
return ret;
}
return _oldmap.apply(this, arguments);
}
})(jQuery.map);
}
// Compressed
/*
parseFloat(jQuery.fn.jquery)<1.6&&(jQuery.map=function(a){return function(){var b=arguments[0],c=b.length,d=b instanceof jQuery||c!==undefined&&typeof c=="number"&&(c>0&&b[0]&&b[c-1]||jQuery.isArray(b));if(!d){var e=[];for(key in b)value=arguments[1](b[key],key,arguments[2]),value!=null&&(e[e.length]=value);return e}return a.apply(this,arguments)}}(jQuery.map))
*/
$.map({ 1: 'hi', 'bob': 'joe' }, function(val, key){
console.log(arguments);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment