This question has been resolved.
Based on the answer from karuzo, I made a finalized version:
$['to'] = function ($a)
{
$u = JSON.stringify(this); $w = this.typeof(); $x = $a.typeof();
if ($x == 'Array')
{
if ($w == 'Object')
{
$y = []; this.keys().for(($l, $m, $n) =>
{
$y[$m] = [$l, this[$l]];
});
return $y;
}
else if ($w == 'Array')
{
return [].slice.call(this);
}
else if ($w == 'String')
{
return [this];
};
}
else if ($x == 'Object')
{
if ($w == 'Object')
{
return this.to("").from({});
}
else if ($w == 'Array')
{
$y = {}; this.for(($l, $m, $n) =>
{
$l != {}._ ? $y[$m] = $l : null;
});
return $y;
}
else if ($w == 'String')
{
return {value: this};
}
}
else if ($x == 'String')
{
if ($w == 'Object')
{
return JSON.stringify(this).replace(/\"([^(\")"]+)\":/g, '$1:');
}
else if ($w == 'Array')
{
return $u;
}
else if ($w == 'String' | $w == 'Number')
{
return this.toString();
};
}
else
{
return this;
};
}
You can try:
var obj = {a: "x", b: ["y"], c: {d: ["z"]}, e: [{f: "g"}]};
var objStr = JSON.stringify(obj).replace(/\"([^(\")"]+)\":/g,"$1:");
console.log(objStr);
Output:
{a:"x",b:["y"],c:{d:["z"]},e:[{f:"g"}]}
And you can eval()
the resulting string:
var obj = eval(objStr)