But a much simpler approach to getting the data would be:
Code:
Array.prototype.enumerate = function(){
for(var o = {}, i = 0; i < this.length; ++i)
o[this[i]] = o[this[i]]? ++o[this[i]] : 1;
return o;
};
Which could be formatted more or less as before, or using this more elaborate and better looking method:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
#result {
font: normal 90% sans-serif;
}
#result .rhspan {
float: right;
}
#result br {
clear: right;
}
</style>
<script type="text/javascript">
Array.prototype.enumerate = function(){
for(var o = {}, i = 0; i < this.length; ++i)
o[this[i]] = o[this[i]]? ++o[this[i]] : 1;
return o;
};
function displayResults(ar, el){
el = document.getElementById(el);
while (el.lastChild) el.removeChild(el.lastChild);
var o = ar.enumerate(), p, s0, s1, br, w = 0;
for (p in o){
w = Math.max( w, (p + o[p]).length + 3 );
s0 = document.createElement('span');
s1 = document.createElement('span');
br = document.createElement('br');
s1.className = 'rhspan';
s0.appendChild(document.createTextNode(p));
s1.appendChild(document.createTextNode('(' + o[p] + ')'));
el.appendChild(s1);
el.appendChild(s0);
el.appendChild(br);
};
el.style.width = w + 'ex';
};
var someArray = new Array("Bob", "Twey", "Bob", "Twey", "John", "Mary", "Steve", "Mary", "Twey", "A real long name here", 0, 0, 257);
</script>
</head>
<body>
<div>
<input type="button" value="Go!" onclick="displayResults(someArray, 'result');">
<div id="result">
Results Will Appear Here
</div>
</div>
</body>
</html>
Bookmarks