Nodejs Array Convert toSentence(connector)

Here you can find the source of toSentence(connector)

Method Source Code

/**//from   w w w  .  j ava  2  s .c o  m
 * @class Array
 * Extensions to the array class
 */

/**
 * Turns an array into a sentence, joined by a specified connector - e.g.:
 * ['Adama', 'Tigh', 'Roslin'].toSentence(); //'Adama, Tigh and Roslin'
 * ['Adama', 'Tigh', 'Roslin'].toSentence('or'); //'Adama, Tigh or Roslin'
 * @param {String} connector The string to use to connect the last two words. Usually 'and' or 'or', defaults to 'and'
 */
Array.prototype.toSentence = function(connector) {
  connector = connector || 'and';
  
  var sentence = "";
  if (this.length <= 1) { 
    sentence = this[0];
  } else {
    //we'll join all but the last error with commas
    var firstErrors = this.slice(0, this.length - 1);
    
    //add the last error, with the connector string
    sentence = String.format("{0} {1} {2}", firstErrors.join(", "), connector, this[this.length - 1]);
  }
  return sentence;
};

Related

  1. toOneThousand()
    Array.prototype.toOneThousand = function() {
      for (var i = 1; i < 1001; i++) {
        this.push(i);
      return this;
    },
    
  2. toOneThousand()
    Array.prototype.toOneThousand = function(){
        var start = 10;
        var end = 1000;
        for(var i = start; i <= end; i += 10){
            this.push(i);
       return this;
    var oneToOneThousand = [].toOneThousand();
    ...
    
  3. toOneThousand()
    Array.prototype.toOneThousand = function() {
      return Array.range(1000, 10);
    };
    
  4. toOneThousand()
    Array.prototype.toOneThousand = function(){
      b = [];
      for(i=10; i<=1000; i+=10){
        b.push(i);
      return b
    
  5. toOneThousand()
    Array.prototype.toOneThousand = function() {
      'use strict';
      var arr = [];
      for (var i = 10; i <= 1000; i += 10) {
        arr.push(i);
      return arr;
    };
    
  6. toSet()
    Array.prototype.toSet = function () {
        var temp = [];
        this.forEach(function (e) {
            if (temp.indexOf(e) === -1)
                temp.push(e);
        });
        return temp;
    
  7. toSet()
    Array.prototype.toSet = function(){
      var set = [];
      for (var i=0; i<this.length; i++){
        if (!set.contains(this[i])){
          set.push(this[i]);
      return set;
    
  8. toSource(seen)
    Array.prototype.toSource = function(seen){
      var source, i = 0, j = this.length;
      seen = seen || [this];
      source = '[';
      for(;i<j;i++){
        source+= Object.sourceOf(this[i], seen, '[]');
        if( i < j - 1 ) source+= ', ';
      source+= ']';
    ...
    
  9. toStrGen(originStr)
    Array.prototype.toStrGen=function(originStr){
      if(Object.prototype.toString.call(originStr).slice(8,-1)==="Array"){
        return this.map(function(i){
          return originStr.join(i);
        }).reduce(function(m,n){return m+n+'\b'}, "");
    };