-
-
Save jed/964771 to your computer and use it in GitHub Desktop.
map properties for arrays and objects
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
function( | |
a, // source object or array | |
b, // map function | |
c, // (placeholder) | |
d, // (placeholder) | |
e // (placeholder) | |
){ | |
c = a.length; // get the length of the object. | |
if ( // it's an array if | |
c >= 0 // the length is zero or a positive integer, | |
) for ( // so loop by | |
e = [], // caching the target array, | |
d = 0; // initializing the cursor, and | |
d < c; // until the cursor reaches the length | |
d++ // by increments, | |
) e[d] = // push to the array | |
b(a[d], d); // the results of the map function | |
else { // otherwise, if the object has no length | |
e = {}; // cache the target object, and | |
for ( // for | |
d // each property | |
in a // in the source object | |
) e[d] = // set the same on the target object with | |
b(a[d],d) // the results of the map function | |
} | |
return e // finally, return the target object | |
} |
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
function(a,b,c,d,e){c=a.length;if(c>=0)for(e=[],d=0;d<c;d++)e[d]=b(a[d],d);else{e={};for(d in a)e[d]=b(a[d],d)}return e} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Jed Schmidt <http://jed.is> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "map", | |
"keywords": ["map", "array", "arrays", "functional"] | |
} |
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
// Define the map function | |
var map = function(a,b,c,d,e){c=a.length;if(c>=0)for(e=[],d=0;d<c;d++)e.push(b(a[d],d));else{e={};for(d in a)e[d]=b(a[d],d)}return e} | |
// Define an array and a object | |
var arr = ["a", "b", "c", "d", "e"] | |
var obj = {a:0, b:1, c:2, d:3, e:4} | |
// Map each, returing the key/val at each | |
arr = map( arr, function( value, index ){ return index + value } ) | |
obj = map( obj, function( value, index ){ return index + value } ) | |
// Log the results: | |
console.log( arr ) // => [ '0a', '1b', '2c', '3d', '4e' ] | |
console.log( obj ) // => { a: 'a0', b: 'b1', c: 'c2', d: 'd3', e: 'e4' } |
Hey, sorry for the piecemeal suggestions, but found another optimization... Now that you got rid of .push
, instead of checking for e's type and doing either e = [],
or e={},
, you can just do e=a;
after c=a.length
to capture the appropriate type and then e[d]=
will just overwrite the values. Saves another 6 bytes!
Woah, and then, if you do that, you can get rid of the if/else, and replace with ternary! Forking to test... brb.
Yeah, so for...in works on arrays too. Got this to be 58 bytes down from 121. :D https://gist.github.com/1012114 Not sure if this is cross-browser compatible or not, but I assume it would be
The array fork needs to check for sparse arrays and the object fork should check for own properties.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good call, mike. fixed!