Nodejs Array Item Occurrence numberOfOccurrences(i)

Here you can find the source of numberOfOccurrences(i)

Method Source Code

Array.prototype.numberOfOccurrences = function(i) {
  var total = 0;//from  w  w  w  . j  ava 2 s  . co  m
  this.filter(function (x) {
    if (i === x)
      total++;
  });
  return total;
};

var arr = [0,1,2,2,3];

console.log(arr.numberOfOccurrences(0));// === 1;
console.log(arr.numberOfOccurrences(4));// === 0;
console.log(arr.numberOfOccurrences(2));// === 2;
console.log(arr.numberOfOccurrences("a"));// === 0;

// Top solution:
//
// Array.prototype.numberOfOccurrences = function(n) {
//   return this.filter(function(x){return x==n;}).length;
// }
//

Related

  1. numberOfOccurrences(arr)
    Array.prototype.numberOfOccurrences = function (arr) {
        var elem = 0;
        for (var i = 0; i < this.length; i++) {
            if (this[i] === arr) {
                elem++;
        return elem;
    };
    ...
    
  2. numberOfOccurrences(chr)
    Array.prototype.numberOfOccurrences = function(chr) {
      return this.filter(function(x){ return x == chr; }).length;
    
  3. numberOfOccurrences(desVal)
    Array.prototype.numberOfOccurrences = function(desVal) {
      var counter = 0;
      this.forEach(function(value){
        if(value === desVal){
          counter++;
      });
      return counter;
    };
    ...
    
  4. numberOfOccurrences(el)
    Array.prototype.numberOfOccurrences = function (el) {
      var count = 0;
      this.forEach(function (item) {
        if (item === el) count++;
      });
      return count;
    
  5. numberOfOccurrences(elem)
    Array.prototype.numberOfOccurrences = function(elem) {
      let obj = {};
      this.forEach(elem => elem in obj ? obj[elem]++ : obj[elem] = 1);
      return obj[elem] === undefined ? 0 : obj[elem]
    
  6. numberOfOccurrences(item)
    Array.prototype.numberOfOccurrences = function(item) {
    var drr = []; 
    var i = 0 ;
       for(i = 0 ; i < this.length ; i++) {
           if(item=== this[i]){
               drr.push(this[i]); 
        return drr.length;
    ...
    
  7. numberOfOccurrences(item)
    var arr = [4, 0, 4];
    Array.prototype.numberOfOccurrences = function(item) {
      tmp = 0;
      for(var i=0; i<this.length; i++){
        if (item == this[i]) {
          tmp ++;
      console.log(tmp);
    ...
    
  8. numberOfOccurrences(n)
    Array.prototype.numberOfOccurrences = function(n) {
      var counter = 0;
      for (i = 0; i < this.length; i++){
        if (n == this[i]){
          counter++;
      return counter;
    
  9. numberOfOccurrences(num)
    Array.prototype.numberOfOccurrences = function(num) {
        return this.filter(function(item){return item==num;}).length;