Array.prototype.countAll

by Luis Martin

JavaScript

let rep=["ccc","vvv","sss","vvv","ccc","vvv"];

Array.prototype.countElem = function(elem){
	return this.filter(el => el == elem).length;
}

console.log(rep.countElem("vvv"))


Array.prototype.countAll = function() {
  let set = new Set();
  this.forEach(elem => set.add(elem));
  return JSON.stringify(Array.from(set).map(elem => ({ [elem] : this.countElem(elem)})));
}

console.log(rep.countAll())