Nodejs Array Sum sum(selectorfun)

Here you can find the source of sum(selectorfun)

Method Source Code

/**//from w ww  .j  a  va 2  s.co  m
 * Return the sum of all the values in the array. If selectorfun is given,
 * it will be called to retrieve the relevant value for each item in the
 * array.
 */
Array.prototype.sum = function(selectorfun)
{
  if (selectorfun)
  {
    return this.map(selectorfun).sum();
  }
  else
  {
    var ret = 0;
    this.forEach(function(e) { ret += e });
    return ret
  }
};

Array.prototype.unique = function()
{
  return this.reduce(function(list, e)
  {
    if (list.indexOf(e) == -1)
      list.push(e);

    return list;
  }, []);
}

Array.prototype.__defineGetter__("last", function()
{
   return this[this.length - 1];
});

Array.prototype.__defineSetter__("last", function() {});

Array.prototype.extend = function(list)
{
  this.push.apply(this, list);
  return this;
};

Array.prototype.insert = function(index, list, replace_count)
{
  this.splice.apply(this, [index, replace_count || 0].extend(list));
  return this;
};

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

Related

  1. sum(fun)
    Array.prototype.sum = function (fun) {
        var result = 0;
        this.forEach(x => result += fun ? fun(x) : x);
        return result;
    };
    
  2. 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) {
    ...
    
  3. 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;
    };
    
  4. 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;
    };
    
  5. sum(selector)
    Array.prototype.sum = function(selector) {
        if (typeof selector !== 'function') {
            selector = function(item) {
                return item;
        var sum = 0;
        for (var i = 0; i < this.length; i++) {
            sum += selector(this[i]);
    ...
    
  6. sum(xAndY)
    Array.prototype.sum = function(xAndY){
      var count = 0;
      for(var i = 0; i < this.length; i++)
        count += xAndY(this[i])
      return count;
    
  7. 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());
    
  8. 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];
    ...