Nodejs Array Convert toObject()

Here you can find the source of toObject()

Method Source Code

/**// www.j  av a  2  s .c o m
 * Add to Object method to JavaScript Array class
 * @returns {{}}
 */
Array.prototype.toObject = function() {
   var object = {};
   for (var i = 0; i < this.length; ++i)
      object[i] = this[i];
   return object;
}

Related

  1. toList(separator, aggregator)
    Array.prototype.toList = function(separator, aggregator) {
      aggregator = aggregator || separator;
      return this.join(aggregator, [this.join(separator, this.slice(0, -1)), this.slice(-1)]);
    };
    
  2. toNumFixed(num)
    Array.prototype.toNumFixed = function (num) {
      for (var i = 0; i < this.length; i++) {
        this[i] = this[i].toNumFixed(num);
      return this;
    
  3. toNumber()
    Array.prototype.toNumber = Array.prototype.toNumber || function () {
      let result;
      this.forEach(digit => {
        let power = 10;
        while (digit >= power) {
          power *= 10;
        result = ((result || 0) * power) + digit;
      });
    ...
    
  4. toNumbers()
    Array.prototype.toNumbers = function() {
      for (var i=0; i<this.length;i++) {
        var val = Number(this[i]);
        if (!isNaN(val)) {
          this[i] = val;
        } else {
          this[i] = 0;
      return this;
    };
    
  5. toNumsOnly()
    Array.prototype.toNumsOnly = function() {
      return this.filter(el => !parseInt(el, 10).isNotNumber());
    };
    
  6. toObject()
    'use strict';
    Array.prototype.toObject = function() {
      var arr = this,
        res = { } ;
      for (var i = 0 ; i < arr.length ; i++) {
        res[ arr[i] ] = arr[i]
      return res
    
  7. toObject()
    Array.prototype.toObject = function() {
      var resultObject = {};
      this.map(function (element, index) {
        resultObject[index] = element;
      });
      return resultObject;
    };
    
  8. toObject(projection)
    Array.prototype.toObject = function (projection) {
      var result = {};
      for (var i = 0; i < this.length; i++)
        result[this[i][projection]] = this[i];
      return result;
    };
    
  9. toOneThousand()
    Array.prototype.toOneThousand = function() {
      for (var i = 1; i < 1001; i++) {
        this.push(i);
      return this;
    },