Nodejs Utililty Methods Array Convert

List of utility methods to do Array Convert

Description

The list of methods to do Array Convert are organized into topic(s).

Method

toJSON()
Array.prototype.toJSON = function(){
    var result = [];
    this.forEach(function(item,index){
        result.push( item.toJSON ? item.toJSON() : item );
    });
    return result;
toJson()
Array.prototype.toJson = function(){
  return JSON.stringify(this);
};
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)]);
};
toNumFixed(num)
Array.prototype.toNumFixed = function (num) {
  for (var i = 0; i < this.length; i++) {
    this[i] = this[i].toNumFixed(num);
  return this;
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;
  });
...
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;
};
toNumsOnly()
Array.prototype.toNumsOnly = function() {
  return this.filter(el => !parseInt(el, 10).isNotNumber());
};
toObject()
Array.prototype.toObject = function() {
  var object = {};
  for (var i = 0; i < this.length; ++i)
    object[i] = this[i];
  return object;
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
toObject()
Array.prototype.toObject = function() {
  var resultObject = {};
  this.map(function (element, index) {
    resultObject[index] = element;
  });
  return resultObject;
};