Question

What's wront with this piece of code:

function obj2string(obj) {
    var result = '';

    for(var i in obj) {
        if(typeof(obj[i]) === 'object') {
            result += obj2string(obj[i]);
        } else {
            result += i + " => " + obj[i] + "\n";
        }
    }

    return result;
}

It's supposed to recursively concentate the result string with new properties, however there's at somepoint too much recursion.

I was passing an object like this: $(this); -> from jQuery.

$(this)

Being an instance of this jQuery selector: $('.debug'); witch has one class matched in the current document.

Was it helpful?

Solution

if(typeof(obj[i]) === 'object') { will execute if obj[i] is null. Are you aware of that? Try it with $.isPlainObject() (source)

OTHER TIPS

var s = JSON.stringify(obj, null, 4);

You almost certainly have a circular reference (i.e. one of the properties of the input object (or one of those property's properties, and so on)) references another property in the structure that leads back to itself.

A moment's thought should reveal why this can't possibly work.

Calling JSON.stringify( jQueryObject ) in the Chrome console gives a "circular_structure" error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top