JavaScript array_count_values
Return the value as key and the frequency of that value in input as value
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 50 | function array_count_values (array) { // Return the value as key and the frequency of that value in input as value // // version: 909.322 // discuss at: http://phpjs.org/functions/array_count_values // + original by: Ates Goral (http://magnetiq.com) // + namespaced by: Michael White (http://getsprink.com) // + input by: sankai // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]); // * returns 1: {3:2, 5:1, "foo":2, "bar":1} // * example 2: array_count_values({ p1: 3, p2: 5, p3: 3, p4: "foo", p5: "bar", p6: "foo" }); // * returns 2: {3:2, 5:1, "foo":2, "bar":1} // * example 3: array_count_values([ true, 4.2, 42, "fubar" ]); // * returns 3: {42:1, "fubar":1} var tmp_arr = {}, key = '', t = ''; var __getType = function (obj) { // Objects are php associative arrays. var t = typeof obj; t = t.toLowerCase(); if (t == "object") { t = "array"; } return t; }; var __countValue = function (value) { switch (typeof(value)) { case "number": if (Math.floor(value) != value) { return; } case "string": if (value in this) { ++this[value]; } else { this[value] = 1; } } }; t = __getType(array); if (t == 'array') { for ( key in array ) { __countValue.call(tmp_arr, array[key]); } } return tmp_arr; } |
Examples
» Example 1
Running
1 | array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]); |
Should return
1 | {3:2, 5:1, "foo":2, "bar":1} |
» Example 2
Running
1 | array_count_values({ p1: 3, p2: 5, p3: 3, p4: "foo", p5: "bar", p6: "foo" }); |
Should return
1 | {3:2, 5:1, "foo":2, "bar":1} |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have array_count_values goodness in JavaScript.
Hi,
I am on firefox on a mac. This code doesn't seem to be working. I tried debugging it by putting alerts in the function. It looks ok except it doesn't return what I expect it to. I am using your example to try to produce the same result to no avail.
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 80 | <script type='text/javascript'> function array_count_values( array ) { // http://kevin.vanzonneveld.net // + original by: Ates Goral (http://magnetiq.com) // + namespaced by: Michael White (http://getsprink.com) // + input by: sankai // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]); // * returns 1: {3:2, 5:1, "foo":2, "bar":1} // * example 2: array_count_values({ p1: 3, p2: 5, p3: 3, p4: "foo", p5: "bar", p6: "foo" }); // * returns 2: {3:2, 5:1, "foo":2, "bar":1} // * example 3: array_count_values([ true, 4.2, 42, "fubar" ]); // * returns 3: {42:1, "fubar":1} var tmp_arr = {}, key = '', t = ''; var __getType = function(obj) { // Objects are php associative arrays. var t = typeof obj; t = t.toLowerCase(); if (t == "object") { t = "array"; } return t; } var __countValue = function (value) { switch (typeof(value)) { case "number": if (Math.floor(value) != value) { return; } case "string": if (value in this) { ++this[value]; } else { this[value] = 1; } } }; t = __getType(array); if (t == 'array') { for ( key in array ) { __countValue.call(tmp_arr, array[key]); } } return tmp_arr; } function formValidator(){ var tmpArray= []; tmpArray = array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]); document.write(tmpArray[0] + tmpArray[1]+ tmpArray[2]); document.write(tmpArray[3] + tmpArray[4]+ tmpArray[5]); // document.write(tmpArray[0] + " " + tmpArray[1]+ " " + tmpArray[2] + " " + tmpArray[3]+ " " + tmpArray[4]+ " " + tmpArray[5]); } </script> <form id="frmchoixronde1" name="frmchoixronde1" method="POST" enctype="application/x-www-form-urlencoded" onsubmit='return formValidator()'> <html><head><title>Page des poolers</title></head> <body><b>Entrez vos choix pour la premiere ronde</b> </br><fieldset> <b><label for="player_1" style="width:2em">1</label></b><input name="player_1" id="player_1" type="text" size="30"></br> <b><label for="player_2" style="width:2em">2</label></b><input name="player_2" id="player_2" type="text" size="30"></br><b><label for="player_3" style="width:2em">3</label></b><input name="player_3" id="player_3" type="text" size="30"></br> <b><label for="player_4" style="width:2em">4</label></b><input name="player_4" id="player_4" type="text" size="30"></br> <b><label for="player_5" style="width:2em">5</label></b><input name="player_5" id="player_5" type="text" size="30"></br> <b><label for="player_6" style="width:2em">6</label></b><input name="player_6" id="player_6" type="text" size="30"></br><input type="submit" name="submit" value="Soumettre vos choix"> </form> </br> </body></html> |
@ sankai: Thanks for pointing that out sankai! I've replaced the mozilla-only: forEach with a regular for loop. It should work fine now!
It seems because IE broswer don't support the mothod array.forEach().I search some solution from the google web.
http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach
I try add the code before array_count_values()
1 2 3 4 56 7 8 9 1011 12 13 14 1516 | if (!Array.prototype.forEach) { Array.prototype.forEach = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) fun.call(thisp, this[i], i, this); } };} |
lol...It still can't work!!!
Orz..do you have any idea,sir?
It can't work well in IE!
The debug infomation is "Object don't support the attribute or method" in the code as
1
2
3
4
56
7
| if (array instanceof Array) { array.forEach(countValue, tmp_ar); } else if (array instanceof Object) { for ( key in array ) { countValue.call(tmp_ar, array[key]); } } |
å›§rz..but,It's working very well in Firefox!!


Kevin van Zonneveld
3 Apr '09
$ ./phpjstest.php array_count_values
array/array_count_values.js returns#1 OKAY
returns#2 OKAY
returns#3 OKAY
Could you provide the exact code that fails? What did you expect, and what did it return instead? That would help us fix the problem! Thx