Created
August 7, 2010 00:31
-
-
Save robertsosinski/512261 to your computer and use it in GitHub Desktop.
Map-Reduce Example
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
var rows = ["alice apple", "bob banana", "charlie cherry", "alice date", "bob eggplant"]; | |
var mapped = rows.map(function(row) { | |
return row.split(" "); | |
}); | |
var reduced = mapped.reduce(function(hash, pair) { | |
var key = pair[0], | |
val = pair[1]; | |
hash[key] ? hash[key].push(val) : hash[key] = [val] | |
return hash; | |
}, {}); | |
// {"alice": ["apple", "date"], "bob": ["banana", "eggplant"], "charlie": ["cherry"]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment