Skip to content

Instantly share code, notes, and snippets.

@zykadelic
Last active June 10, 2022 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zykadelic/5069223 to your computer and use it in GitHub Desktop.
Save zykadelic/5069223 to your computer and use it in GitHub Desktop.
Count the frequency of an element within an array, returns length if the argument is undefined
if(!Array.prototype.count){
Array.prototype.count = function(el){
if(typeof(el) === "undefined"){
return this.length;
}else{
var count = 0;
for(i = 0; i < this.length; i++){
if(this[i] === el){
count++;
}
}
return count;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment