Object Dump JavaScript

Javascript

Javascript Problem Overview


Is there a 3rd party add-on/application or some way to perform object map dumping in script debugger for a JavaScript object?

Here is the situation... I have a method being called twice, and during each time something is different. I'm not sure what is different, but something is. So, if I could dump all the properties of window (or at least window.document) into a text editor, I could compare the state between the two calls with a simple file diff. Thoughts?

Javascript Solutions


Solution 1 - Javascript

console.log("my object: %o", myObj)

Otherwise you'll end up with a string representation sometimes displaying:

[object Object]

or some such.

Solution 2 - Javascript

Firebug + console.log(myObjectInstance)

Solution 3 - Javascript

function mydump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') {  
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { 
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += mydump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { 
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

Solution 4 - Javascript

If you're on Chrome, Firefox or IE10 + why not extend the console and use

(function() {
    console.dump = function(object) {
        if (window.JSON && window.JSON.stringify)
            console.log(JSON.stringify(object));
        else
            console.log(object);
    };
})();

for a concise, cross-browser solution.

Solution 5 - Javascript

Just use:

console.dir(object);

you will get a nice clickable object representation. Works in Chrome and Firefox

Solution 6 - Javascript

for better readability you can convert the object to a json string as below:

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Solution 7 - Javascript

For Chrome/Chromium

console.log(myObj)

or it's equivalent

console.debug(myObj)

Solution 8 - Javascript

Using console.log(object) will throw your object to the Javascript console, but that's not always what you want. Using JSON.stringify(object) will return most stuff to be stored in a variable, for example to send it to a textarea input and submit the content back to the server.

Solution 9 - Javascript

In Chrome, click the 3 dots and click More tools and click developer. On the console, type console.dir(yourObject).Click this link to view an example image

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJessy HouleView Question on Stackoverflow
Solution 1 - JavascriptTimView Answer on Stackoverflow
Solution 2 - JavascriptDarin DimitrovView Answer on Stackoverflow
Solution 3 - JavascriptzhongshuView Answer on Stackoverflow
Solution 4 - JavascriptChris HGView Answer on Stackoverflow
Solution 5 - Javascriptmons droidView Answer on Stackoverflow
Solution 6 - JavascriptArisView Answer on Stackoverflow
Solution 7 - JavascriptkaznovacView Answer on Stackoverflow
Solution 8 - JavascriptSebastianView Answer on Stackoverflow
Solution 9 - JavascriptRoy SelimView Answer on Stackoverflow