-
-
Save mikesherov/1012114 to your computer and use it in GitHub Desktop.
map properties for arrays and objects
This file contains hidden or 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) | |
){ | |
d = a; // copy the object to the result, keys will be overwritten shortly | |
for ( // for | |
c // each property | |
in a // in the source object.. works on both arrays and objects | |
) d[c] = // set the same on the target object with | |
b(a[c],c); // the results of the map function | |
return d // finally, return the target object | |
} |
This file contains hidden or 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){d=a;for(c in a)d[c]=b(a[c],c);return d} |
This file contains hidden or 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 Mike Sherov <http://mike.sherov.com> | |
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 hidden or 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 hidden or 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){d=a;for(c in a)d[c]=b(a[c],c);return d} | |
// 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' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
well, ideally, i'd like to have something that would roll map and filter into one for arrays and objects, where members could be mapped to a number of members from 0 to 1 for the object case and 0 to n for the array case.
@mathiasbynens, do you have any thoughts on this?