---
layout: page
title: "JavaScript array_count_values function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/array_count_values:308
- /functions/view/array_count_values
- /functions/view/308
- /functions/array_count_values:308
- /functions/308
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's array_count_values

{% codeblock array/array_count_values.js lang:js https://raw.github.com/kvz/phpjs/master/functions/array/array_count_values.js raw on github %}
function array_count_values (array) {
  // From: http://phpjs.org/functions
  // +   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)
  // +   input by: Shingo
  // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
  // *     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;
      }
      // Fall-through
    case "string":
      if (value in this && this.hasOwnProperty(value)) {
        ++this[value];
      } else {
        this[value] = 1;
      }
    }
  };

  t = __getType(array);
  if (t === 'array') {
    for (key in array) {
      if (array.hasOwnProperty(key)) {
        __countValue.call(tmp_arr, array[key]);
      }
    }
  }

  return tmp_arr;
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/array/array_count_values.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/array/array_count_values.js)

### Example 1
This code
{% codeblock lang:js example %}
array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
{3:2, 5:1, "foo":2, "bar":1}
{% endcodeblock %}

### Example 2
This code
{% codeblock lang:js example %}
array_count_values({ p1: 3, p2: 5, p3: 3, p4: "foo", p5: "bar", p6: "foo" });
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
{3:2, 5:1, "foo":2, "bar":1}
{% endcodeblock %}

### Example 3
This code
{% codeblock lang:js example %}
array_count_values([ true, 4.2, 42, "fubar" ]);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
{42:1, "fubar":1}
{% endcodeblock %}


### Other PHP functions in the array extension
{% render_partial _includes/custom/array.html %}
