Browser MapReduce + Charting
Having dabbled in Mongo a bit, I was curious if there was an analog for MapReduce style functionality in browser JavaScript. I found a small script as a starting point, but made some minor modifications to support array inputs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
// mapper should return an array of [{key:'somekey', value:'somevalue'}] // reducer should return a single {key:'somekey', value:'somevalue'} function mapReduce(i, mapper, reducer, property) { var intermediate = []; var output = []; if(i && i.constructor == Array) { for(var x = 0; x < i.length; x++) { var value = i[x][property]; var key = property; intermediate = intermediate.concat(mapper(key, value)); } } else { for (var key in i) { var value = i[key]; intermediate = intermediate.concat(mapper(key, value)); } } var groups = groupBy(intermediate); for (var key in groups) { var values = groups[key]; output.push(reducer(key, values)); } return output; } // list should be [{key:k, value:v}, ....] where key may be repeated. // returns [{key, [v1, v2, v3...]}, ...] where key is *not* repeated. function groupBy(list) { var ret = {}; for (var i = 0; i < list.length; i++) { var key = list[i].key; var value = list[i].value; if (!ret[key]) { ret[key] = []; } ret[key].push(value); } return ret; } |
...