Nodejs Array Sum sum(selector)

Here you can find the source of sum(selector)

Method Source Code

Array.prototype.sum = function(selector) {
    if (typeof selector !== 'function') {
        selector = function(item) {
            return item;
        }/*from   w ww.j  a v a2  s  .co  m*/
    }
    var sum = 0;
    for (var i = 0; i < this.length; i++) {
        sum += selector(this[i]);
    }
    return sum;
};

String.prototype.contains = function(it) {
    return this.indexOf(it) != -1; 
};

Related

  1. sum(begin, end)
    Array.prototype.sum = function(begin, end) {
      var total = 0;
      for (var i = begin >> 0 || 0, len = end + 1 >> 0 || this.length >> 0; i < len; i++) {
        total += +this[i] || 0;
      return total;
    };
    
  2. sum(fun)
    Array.prototype.sum = function (fun) {
        var result = 0;
        this.forEach(x => result += fun ? fun(x) : x);
        return result;
    };
    
  3. sum(func)
    Array.prototype.sum = function (func) {
        var length = this.length,
            sum = 0;
        for (var i = 0; i < length; i++) {
            sum += func(this[i]);
        return sum;
    function isUndefinedNullOrEmpty(item) {
    ...
    
  4. sum(prop)
    Array.prototype.sum = function (prop) {
        var total = 0;
        for ( var i = 0, _len = this.length; i < _len; i++ ) {
            total += this[i][prop]
        return total;
    };
    
  5. sum(s)
    Array.prototype.sum = function (s) {
      s = s || Selector;
      var l = this.length;
      var sum = 0;
      while (l-- > 0) sum += s(this[l]);
      return sum;
    };
    
  6. sum(selectorfun)
    Array.prototype.sum = function(selectorfun)
      if (selectorfun)
        return this.map(selectorfun).sum();
      else
        var ret = 0;
    ...
    
  7. sum(xAndY)
    Array.prototype.sum = function(xAndY){
      var count = 0;
      for(var i = 0; i < this.length; i++)
        count += xAndY(this[i])
      return count;
    
  8. sumArrayItems()
    Array.prototype.sumArrayItems = function() {
      var countIndex = this.length - 1;
      var sum =0;
      while ( countIndex >= 0) {
        sum = sum + this[ countIndex ];
        countIndex--;
      return sum;
    var findMissing = function( arr1 , arr2 ) {
      return Math.abs( arr1 - arr2);
    findMissing([1,2].sumArrayItems() , [1,2,5].sumArrayItems());
    
  9. sumItems()
    Array.prototype.sumItems = function(){
      var sum = 0;
      for(var i=0; i<this.length; i++){
        sum=sum+this[i];
      return(sum);
    var arr = new Array;
    arr = [5,10];
    ...