Perhaps you have been accustomed to Console.log () to debug JS, very useful, but today Weibo see console.table () Debugging JavaScript, and Console.log () similar, the main difference is:
Mainly used to output objects and arrays;
More direct visualization, in tabular form;
You can output one or a few properties individually
Cases
| The code is as follows |
Copy Code |
| <!doctype html> <meta charset= "Utf-8" > <body> <script> /** * * @authors Benjamin * @date 2013-11-18 13:08:18 * Console.table () debugging JavaScript * Directly into the instance */ Define an Object var obj = { "Name": "John", "Age": 20, "Sex": "Male" }; Console.log ("------obj------"); Console.log ("Console.log () Print:"); Console.log (obj); Console.log ("console.table print:"); Console.table (obj);
Define an array var arr = ["AA", "BB", "CC", "DD"]; Console.log ("------arr------"); Console.log ("Console.log () Print:"); Console.log (arr); Console.log ("console.table print:"); Console.table (arr);
Define a multidimensional array var arr = ["AA", "BB", "CC", "DD", ["ee", "FF", "GG", ["HH", "II"]]]; Console.log ("------arr------"); Console.log ("Console.log () Print:"); Console.log (arr); Console.log ("console.table print:"); Console.table (arr);
Define an array of objects var Objarr = [{ "Name": "John", "Age": 20, "Sex": "Male" },{ "Name": "John", "Age": 20, "Sex": "Male" }]; Console.log ("------objarr------"); Console.log ("Console.log () Print:"); Console.log (Objarr); Console.log ("console.table print:"); Console.table (Objarr);
Define a Multilevel Object array var Objgradearr = [{ "Name": "John", "Age": 20, "Sex": "Male", "Attribute": { "A": "AA", "B": "BB", "C": "CC" } },{ "Name": "John", "Age": 20, "Sex": "Male", "Attribute": { "A": "Aa2", "B": "Bb2", "C": "CC2" } }]; Console.log ("------objgradearr------"); Console.log ("Console.log () Print:"); Console.log (Objgradearr); Console.log ("console.table print:"); Console.table (Objgradearr); </script> </body> |
Because I am not good at English translation, directly behind the English address you can enter the reference.
Imagine you have created this list of programming languages and their file extensions:
| The code is as follows |
Copy Code |
var languages = [ {Name: "JavaScript", FileExtension: ". js"}, {name: ' typescript ', fileextension: '. ts '}, {Name: "Coffeescript", FileExtension: ". Coffee"} ]; Console.log (languages); |
The Console.log () call would give you the following representation of your data:
That tree view is helpful for debugging purposes, but I find it a little cumbersome to have to open every object manually. I ' m saying we can do a little better with console.table ().
Logging Array Data with console.table ()
Instead of calling Console.log (), we ll use Console.table () Now:
Console.table (languages);
Make sure the "console is opened before refreshing the" page, otherwise you won ' t to any output. If you are did everything correct, you'll be rewarded with this nice, little table view:
Pretty neat, isn ' t it?
Of course, tables work best for tabular data. If all the objects have a totally different structure, you'll end up with most of the cells containing values. Still, the property values are neatly arranged and give you a good overview.
Logging Object Data with console.table ()
Another nice thing about console.table () are that it also works with objects:
| The code is as follows |
Copy Code |
var languages = { CSharp: {name: "C #", Paradigm: "object-oriented"}, FSharp: {name: "F #", Paradigm: "Functional"} }; |
Console.table (languages);
' Nuff said.
Filtering the displayed Object Properties
If you are want to restrict the columns to certain properties, you can have a array of their keys as a second parameter to th e console.table () Call:
| The code is as follows |
Copy Code |
Multiple Property keys Console.table (languages, ["Name", "Paradigm"]); For a single, a simple string is sufficient: A Single Property key Console.table (Languages, "name"); |
If you do not understand on the, can enter to see English: http://www.mariusschulz.com/2013/11/13/advanced-javascript-debugging-with-consoletable