javascript - Where am I going wrong in my recursive function? -
i working on learning project requires me implement recursive function stringifies passed in object, without using json.stringify. have consider data types parameters function receive, , while fine that, seem getting confused when array/object passed, , call function on iterate on object contents. not doing right , every change make impacting other areas thought had completed frustration starting win battle. areas need on:
output elements of array/object in same way
json.stringifywhen call function on itself. example of issue see is, if pass array["so"], am getting[so]returned, looks have possibility covered!what when function passed param, not allowed.
here have far. appreciate can offer.
var myjsonrecursivefunc = function (obj) { var stringed = ""; var result; if (number.isinteger(obj)) { return "" + obj + ""; } else if (obj === null) { return "" + null + ""; } else if (typeof obj === "boolean") { return "" + obj + ""; } else if (typeof obj === "string") { return '"' + obj + '"'; } else if (array.isarray(obj)) { if (obj.length === 0) { return "[" + obj + "]"; } (var = 0; < obj.length; i++) { if (array.isarray(i)) { myjsonrecursivefunc(i); } else { stringed += "" + obj[i] + "" } } return result = "[" + stringed + "]"; } else { (var val in obj) { if (typeof val === "object") { myjsonrecursivefunc(val); } stringed += "" + val + "" + ":" + "" + obj[val] + "" + ''; } return result = "{" + stringed + "}"; } }; it far perfect still learning please let me know can improve along in getting work is.
output elements of array/object in same way json.stringify when call function on itself. example of issue see is, if pass array ["so"], am getting [so] returned, looks have possibility covered!
your recursion of var val in obj passing in val, key of obj. need call myjsonrecursivefunc(obj[val]) in order right result. additionally, true array. if statement needs check see if obj[i] array, not i integer. in case, need say:
if (array.isarray(obj[i])) { myjsonrecursivefunc(obj[i]) } what when function passed param, not allowed.
you need have check see if function being passed in function, typeof, such as: if (typeof func === function)
this pretty fun exercise. did few months back, , had access underscore library so. here's working code:
var stringifyjson = function(obj) { //strings , null should addressed here. strings have quotes inside string can't lump them booleans , numbers. if (_.isstring(obj)){ return '"' + obj.split('"').join('\\"') + '"'; } if (_.isnull(obj)){ return 'null'; } //arrays temporary array stringified elements pushed to, , temporary array joined , concatenated brackets exist within returned string. if (_.isarray(obj)){ var temparr = []; _.each(obj, function(elem){ temparr.push(stringifyjson(elem)); }); return '[' + temparr.join(',') + ']'; } //objects temporary string add stringified data to. including check undefined values , function keys. if (_.isobject(obj)){ var temparr = []; (var k in obj){ if (_.isundefined(obj[k]) || _.isfunction(k)){ return '{}'; } else { temparr.push(stringifyjson(k) + ':' + stringifyjson(obj[k])); } } return '{' + temparr.join(', ') + '}'; } //everything else = booleans, numbers else { return obj.tostring(); } };
Comments
Post a Comment