How to avoid infinite recursion
Anonymous User
97

Hi,
I had a task to print out all the fields of a given object
and i solved it like this:

function print(jsonObject) {
     if (typeof(jsonObject) === 'object') {
        for (var prop in Object.keys(jsonObject)) {
            if (typeof(jsonObject[prop]) === 'object') {
                print(jsonObject[prop]);
            }else{
				 console.log(prop + ':' + jsonObject[prop]);
			}
        }
    }
}

But what do I do if i had an object refrenced to one another?
like so:

let one = {
	name: 'name',
	age: 21
}

let two = {
	name: {
		firstName: 'name',
		lastName: 'lastname'
	}
	age: 22,
}

two.ref = one
one.ref = two;

What is the stop condition?
thanks

Comments (2)