0

I have a project that requires me to convert an Object to a string, without using stringify. The aim of this is to implement a recursive function that converts an object to a string. I seem to be having 2 problems here:

1) My function to output the Object as a string, only seems to be outputting the first value, not the rest.

function myToString(argObj) {
    var str = "";
    for (var val in argObj) {
        if (argObj.hasOwnProperty(val)) {
            str += val + " : " + argObj[val] + "\n";
            console.log(str);
        }
        return str;
    }
}

2) My understanding of the above for in loop, would be that it would print each key/value pair in the object, but it is only doing so for the first key/value. Using recursion, how can I get this to run over each key/value pair.

4
  • 1
    One of the first things to change is to get rid of document.write(). If you want to trace your code's execution, use console.log() which writes to the developer console. Commented May 29, 2015 at 2:51
  • 1
    The return statement should be moved to the last line. Otherwise it only run one time at for statement. Commented May 29, 2015 at 2:54
  • 1
    Wouldn't have taken long to work out with a debugger hey? Commented May 29, 2015 at 2:56
  • True, but I am in the early stages of a training class that requires I use notepad to get used to stuff like this, so sometimes I miss it. Commented May 29, 2015 at 2:59

2 Answers 2

2

You had your return value inside the for loop, meaning it returned on the first iteration through it. Try this:

function myToString(argObj) {
    var str = "";
    for (var val in argObj) {
        if (argObj.hasOwnProperty(val)) {
            str += val + " : " + argObj[val] + "\n";
            document.write(str);
        }
    }
    return str;
}

After that you want to know if any of the properties of argObj are objects so you can recursively call the function on any of them. From this SO post grab a function to test if a variable is an object. You probably do not want to include arrays in your recursive call. You probably want to print their contents. (but that is another question hey) Then your code becomes something like this:

function myAndSomeGuyOnStackOverflowToString(argObj) {
    var str = "";
    for (var val in argObj) {
        if (argObj.hasOwnProperty(val)) {
            var propertyValue = argObj[val];
            if (isAnObject(propertyValue)){
                propertyValue = myAndSomeGuyOnStackOverflowToString(propertyValue)
            }
            str += val + " : " + propertyValue + "\n";
            document.write(str);
        }
    }
    return str;
}

function isAnObject(objArg){
   //choose your implementation from the many on that other post I mentioned
}

And with some indentation and string formatting you should be well on your way.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks guys, that change worked for me. Do you have any idea how I can make this into a recursive function using an object that is passed? I have gone over many recursive examples online, but cannot see any that cover something like this.
0
function myToString(argObj, res) {

    if ( argObj !== null && typeof argObj === "object") {
        for (var val in argObj)    
            res += val + " : " + argObj[val] + "\n";
    }

    if (typeof argObj === "number" || typeof argObj === "string")
        return res + argObj;

    return res;
}

Invoke this function by calling myToString(object, ""), it returns a string. Hope it can help you or give you some idea to write it recursively.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.