D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Jn1532
Full window
Github gist
JS Bin character frequency part 2 // source http://jsbin.com/kaduruy
<!DOCTYPE html> <html> <head> <meta name="description" content="character frequency part 2"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <script id="jsbin-javascript"> var maxSpeed = { car: 300, bike: 60, motorbike: 200, airplane: 1000, helicopter: 400, rocket: 8 * 60 * 60 }; function getLetterCount(arr){ return arr.reduce(function(prev,next) { prev[next] = (prev[next] + 1) || 1; return prev; },{}); } var xx ="hayjf".split('') ; console.clear(); console.log(getLetterCount(xx)); /////////////////////////////////////////////////////////////////////// var sortable = []; for (var vehicle in maxSpeed) { console.log(vehicle+ ' ' + maxSpeed[vehicle]); sortable.push([vehicle, maxSpeed[vehicle]]); } sortable.sort(function(a, b) { return a[1] - b[1]; }); function compareNumbers(a, b) { return a[1] - b[1]; } console.log('Sorted without a compare function:', sortable.sort()); console.log('Sorted with compareNumbers:', sortable.sort(compareNumbers)); </script> <script id="jsbin-source-javascript" type="text/javascript">var maxSpeed = { car: 300, bike: 60, motorbike: 200, airplane: 1000, helicopter: 400, rocket: 8 * 60 * 60 }; function getLetterCount(arr){ return arr.reduce(function(prev,next) { prev[next] = (prev[next] + 1) || 1; return prev; },{}); } var xx ="hayjf".split('') ; console.clear(); console.log(getLetterCount(xx)); /////////////////////////////////////////////////////////////////////// var sortable = []; for (var vehicle in maxSpeed) { console.log(vehicle+ ' ' + maxSpeed[vehicle]); sortable.push([vehicle, maxSpeed[vehicle]]); } sortable.sort(function(a, b) { return a[1] - b[1]; }); function compareNumbers(a, b) { return a[1] - b[1]; } console.log('Sorted without a compare function:', sortable.sort()); console.log('Sorted with compareNumbers:', sortable.sort(compareNumbers));</script></body> </html>