Nodejs Array Convert toDictionary(key)

Here you can find the source of toDictionary(key)

Method Source Code

Array.prototype.toDictionary = function(key) {
   if (!key)/*w  w w  .ja v  a  2s.co  m*/
      key = function(x) { return x.id; };

   var dictionary = {};
   this.forEach(function(item) {
      var currentKey = key(item);
      if (!currentKey)
         return true;
      if (!dictionary[currentKey])
         dictionary[currentKey] = [];
      dictionary[currentKey].push(item);
   });
   return dictionary;
};

Related

  1. toArray()
    Array.prototype.toArray = function() {
      return this.clone();
    };
    
  2. toArray(obj, start)
    Array.toArray = function(obj, start) {
       return Array.prototype.slice.call(obj, start); 
    };
    
  3. toArrayInt()
    Array.prototype.toArrayInt = function () {
        for (var i=0; i<this.length;i++) {
            this[i] = parseInt(this[i]);
        return this;
    };
    
  4. toColor()
    Array.prototype.toColor = function() {
      var n = this.length <= 3 ? 3 : 4;
      var pieces = this.concat( [0,0,0,0] ).slice(0,n);
      if (!this.length) {
        return "rgb(" + pieces.join() + ")";
      } else if (this.length <= 3) {
        return "rgb(" + pieces.join() + ")";
      } else if (this.length >= 4) {
        return "rgba(" + pieces.join() + ")";
    ...
    
  5. toCommaSeparatedString()
    Array.prototype.toCommaSeparatedString = function() {
      if (this.length > 0) {
        if (this.length == 1) {
          return "" + this[0];
        } else {
          var buf = new StringBuilder();
          for ( var i = 0; i < this.length; i++) {
            if (i > 0) {
              buf.append(",");
    ...
    
  6. toDictionary(keySelector, valueSelector)
    Array.prototype.toDictionary = function (keySelector, valueSelector) {
      var o = {};
      var l = this.length;
      while (l-- > 0) {
        var key = keySelector(this[l]);
        if (key == null || key == "") continue;
        o[key] = valueSelector(this[l]);
      return o;
    ...
    
  7. toFormat(spec)
    Array.prototype.toFormat = function (spec) {
      var buffer = [];
      for (var i = 0, len = this.length; i < len; i++) {
        buffer[i] = Jitsi.format(spec, this[i]);
      return buffer.join('');
    };
    
  8. toForty()
    Array.prototype.toForty = function () {
      for (var i = 1; i < 41; i++) {
        this.push(i);
      return this;
    },
    
  9. toForty()
    Array.prototype.toForty = function(){
        var start = 2;
        var end = 40;
        for(var i = start; i <= end; i += 2){
            this.push(i);
       return this;
    var oneToForty = [].toForty();
    ...