Skip to content

Instantly share code, notes, and snippets.

@robertsosinski
Created August 7, 2010 00:31
Show Gist options
  • Save robertsosinski/512261 to your computer and use it in GitHub Desktop.
Save robertsosinski/512261 to your computer and use it in GitHub Desktop.
Map-Reduce Example
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