Skip to main content
added 323 characters in body
Source Link
Pimp Trizkit
  • 19.8k
  • 5
  • 27
  • 41

Reinventing the wheel here! None of these solutions worked for my situation. So, I quickly doctored up pagewil'swilsonpage's answer. This one is not for printing to screen (via console, or textfield or whatever). It does work fine in those situations and works just fine as the OP requested, for alert. Many answers here do not address using alert as the OP requested. Anyhow, It is, however, formatted for data transport. This version seems to return a very similar result as toSource(). I've not triedtested against JSON.stringify, but I assume this is about the same thing. This version is more like a poly-fil so that you can use it in any environment. The result of this function is a valid Javascript object declaration.

Reinventing the wheel here! None of these solutions worked for my situation. So, I quickly doctored up pagewil's answer. This one is not for printing to screen (via console, or textfield or whatever). It is, however, for data transport. This version seems to return a very similar result as toSource(). I've not tried JSON.stringify, but I assume this is about the same thing. The result of this function is a valid Javascript object declaration.

Reinventing the wheel here! None of these solutions worked for my situation. So, I quickly doctored up wilsonpage's answer. This one is not for printing to screen (via console, or textfield or whatever). It does work fine in those situations and works just fine as the OP requested, for alert. Many answers here do not address using alert as the OP requested. Anyhow, It is, however, formatted for data transport. This version seems to return a very similar result as toSource(). I've not tested against JSON.stringify, but I assume this is about the same thing. This version is more like a poly-fil so that you can use it in any environment. The result of this function is a valid Javascript object declaration.

edited body
Source Link
Pimp Trizkit
  • 19.8k
  • 5
  • 27
  • 41

(This has been added to my library at GitHubGitHub)

(This has been added to my library at GitHub)

(This has been added to my library at GitHub)

added 80 characters in body
Source Link
Pimp Trizkit
  • 19.8k
  • 5
  • 27
  • 41

(This has been added to my library at GitHub)

Reinventing the wheel here! None of these solutions worked for my situation. So, I quickly doctored up pagewil's answer. This one is not for printing to screen (via console, or textfield or whatever). It is, however, for data transport. This version seems to return a very similar result as toSource(). I've not tried JSON.stringify, but I assume this is about the same thing. The result of this function is a valid Javascript object declaration.

functionconst ObjToSourceObjToSource=(o)=> {
    if (!o) return 'null';null;
    let str="",na=0,k,p;
    if (typeof(o) == "object") {
        if (!ObjToSource.check) ObjToSource.check = new Array();
        for (var i=0, k=ObjToSource.check.length ; i<k ; ++ilength;na<k;na++) {
            if (ObjToSource.check[i] == ocheck[na]==o) {return '{}';}
        }
        ObjToSource.check.push(o);
    }
    var k="",na=typeof(o.length)=="undefined"?1:0,str="";0;
    for(var p in o){
        if (na) k = "'"+p+ "'"'"+p+"':";
        if (typeof o[p] == "string") str += k + "'" + o[p]+"'k+"'"+o[p]+"',";
        else if (typeof o[p] == "object") str += k + ObjToSourcek+ObjToSource(o[p])+",";
        else str += k + o[p] + "k+o[p]+",";
    }
    if (typeof(o) == "object") ObjToSource.check.pop();
    if (na) return "{"+str.slice(0,-1)+"}";
    else return "["+str.slice(0,-1)+"]";
}

Reinventing the wheel here! None of these solutions worked for my situation. So, I quickly doctored up pagewil's answer. This one is not for printing to screen (via console, or textfield or whatever). It is, however, for data transport. This version seems to return a very similar result as toSource(). I've not tried JSON.stringify, but I assume this is about the same thing. The result of this function is a valid Javascript object declaration.

function ObjToSource(o){
    if (!o) return 'null';
    if (typeof(o) == "object") {
        if (!ObjToSource.check) ObjToSource.check = new Array();
        for (var i=0, k=ObjToSource.check.length ; i<k ; ++i) {
            if (ObjToSource.check[i] == o) {return '{}';}
        }
        ObjToSource.check.push(o);
    }
    var k="",na=typeof(o.length)=="undefined"?1:0,str="";
    for(var p in o){
        if (na) k = "'"+p+ "':";
        if (typeof o[p] == "string") str += k + "'" + o[p]+"',";
        else if (typeof o[p] == "object") str += k + ObjToSource(o[p])+",";
        else str += k + o[p] + ",";
    }
    if (typeof(o) == "object") ObjToSource.check.pop();
    if (na) return "{"+str.slice(0,-1)+"}";
    else return "["+str.slice(0,-1)+"]";
}

(This has been added to my library at GitHub)

Reinventing the wheel here! None of these solutions worked for my situation. So, I quickly doctored up pagewil's answer. This one is not for printing to screen (via console, or textfield or whatever). It is, however, for data transport. This version seems to return a very similar result as toSource(). I've not tried JSON.stringify, but I assume this is about the same thing. The result of this function is a valid Javascript object declaration.

const ObjToSource=(o)=> {
    if (!o) return null;
    let str="",na=0,k,p;
    if (typeof(o) == "object") {
        if (!ObjToSource.check) ObjToSource.check = new Array();
        for (k=ObjToSource.check.length;na<k;na++) if (ObjToSource.check[na]==o) return '{}';
        ObjToSource.check.push(o);
    }
    k="",na=typeof(o.length)=="undefined"?1:0;
    for(p in o){
        if (na) k = "'"+p+"':";
        if (typeof o[p] == "string") str += k+"'"+o[p]+"',";
        else if (typeof o[p] == "object") str += k+ObjToSource(o[p])+",";
        else str += k+o[p]+",";
    }
    if (typeof(o) == "object") ObjToSource.check.pop();
    if (na) return "{"+str.slice(0,-1)+"}";
    else return "["+str.slice(0,-1)+"]";
}
added 9 characters in body
Source Link
Pimp Trizkit
  • 19.8k
  • 5
  • 27
  • 41
Loading
added 1 characters in body
Source Link
Pimp Trizkit
  • 19.8k
  • 5
  • 27
  • 41
Loading
added 46 characters in body
Source Link
Pimp Trizkit
  • 19.8k
  • 5
  • 27
  • 41
Loading
added 2078 characters in body
Source Link
Pimp Trizkit
  • 19.8k
  • 5
  • 27
  • 41
Loading
added 85 characters in body
Source Link
Pimp Trizkit
  • 19.8k
  • 5
  • 27
  • 41
Loading
added 3 characters in body
Source Link
Pimp Trizkit
  • 19.8k
  • 5
  • 27
  • 41
Loading
Source Link
Pimp Trizkit
  • 19.8k
  • 5
  • 27
  • 41
Loading