Cloning JS Objects for Logging using JSON

If you log a Javascript object to console in a function that gets invoked a larger number of times, and inspect the logs later,
all of them would reflect the state of the object at the point in time when you later expand them. This can be a problem when the object is deep or
has a large number of properties. To solve this, you an capture the state of an object when it was logged by obtaining
the JSON (Javascript object notation) for the Object and use that to create a clone of the object.

console.log(JSON.parse(JSON.stringify(obj)));

Published by anthonykuong

Anthony is a versatile Software professional with around 10 years of experience. He is a Full Stack developer experienced with clients in the Financial, Health and Supply Chain industries. He is experienced with MVC frameworks ( Spring Boot) , SPA frameworks ( Angular , VueJS), and also supports automated build deployments and packaging for development, qa, and production servers.. He has delivered rich user experience using Modern web technologies and techniques such are HTML5, CSS3, ECMAScript 6 (ES6)/ ECMAScript 2015, CSS pre-processors (SASS, Less), JavaScript build tools (Grunt, Gulp) , various UI Frameworks including AngularJS , Knockout JS , and CSS Frameworks including Bootstrap, and Foundation. He is adaptable to new technologies and frameworks. He is a rigorous, quality-conscious contributor with solid analytical skills. I can also be found on youtube - Youtube Channel: https://www.youtube.com/user/akuong/

Leave a comment