Advertisement
leomaster

Array toString

Apr 10th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Array.prototype.toString = function() {
  2.     return (function formatValue(x) {
  3.         if (typeof x == "string") {
  4.             return "'" + x.replace(/\n/g,"\\n") + "'";
  5.         }
  6.         if (typeof x == "number" && x === 0 && (1/x === -Infinity)) {
  7.             return "-0";
  8.         }
  9.         if (Array.isArray(x)) {
  10.             return "[" + x.map(formatValue).join(",") + "]";
  11.         }
  12.         if (x !== null && typeof x == "object") {
  13.             return "{" + Object.keys(x).reduce(function(r,k){
  14.                 if (k != "toString" && k != "valueOf") {
  15.                     r.push("'" + k + "':" + formatValue(x[k]));
  16.                 }
  17.                 return r;
  18.             },[]).join(",") + "}";
  19.         }
  20.         return String(x);
  21.     })(this);
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement