0

I have this code so far:

function print_r( obj ) {
    if((typeof(obj)) == 'object' ) {
        var output = [];

        for(var x in obj) {
            if((typeof(obj[x])) == 'object') {
                output.push( print_r(obj[x]) );
            } else {
                output.push( x + ' => "' + obj[x].toString() + '"' );
            }
        }

        return output.join("\n");
    }

    return obj;
}

It does some basic recursion, but it's not good at this point.

How can I make this JavaScript function behave as PHP's print_r function?

EDIT: With tab indents if the loop accours on a new object within the parent object.

4

1 に答える 1