Use console. table () to debug javascript

Source: Internet
Author: User

I have seen a foreign user using console. table () to debug javascript code. I feel this is very good. Next I will introduce you to using console. table () to debug javascript instances.

Maybe you are used to console. log () to debug js, very easy to use, but today Weibo saw the console. table () debugging javascript, and console. log () is similar, the main difference is:

It is mainly used to output objects and arrays;
More direct visualization, presented in the form of tables;
One or more attributes can be output separately.

Example

The Code is as follows: Copy code

<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
</Head>
<Body>
<Script>
/**
*
* @ Authors Benjamin
* @ Date 2013-11-18 13:08:18
* Console. table () debug javascript
* Directly go to the instance
*/
// Define an object
Var obj = {
"Name": "Zhang San ",
"Age": 20,
"Sex": "male"
};
Console. log ("------ obj ------");
Console. log ("console. log () print :");
Console. log (obj );
Console. log ("console. table printing :");
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 printing :");
Console. table (arr );

// Define a multi-dimensional 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 printing :");
Console. table (arr );

// Define an object Array
Var objArr = [{
"Name": "Zhang San ",
"Age": 20,
"Sex": "male"
},{
"Name": "Zhang San ",
"Age": 20,
"Sex": "male"
}];
Console. log ("------ objArr ------");
Console. log ("console. log () print :");
Console. log (objArr );
Console. log ("console. table printing :");
Console. table (objArr );

// Define a multi-level object Array
Var objGradeArr = [{
"Name": "Zhang San ",
"Age": 20,
"Sex": "male ",
"Attribute ":{
"A": "aa ",
"B": "bb ",
"C": "cc"
}
},{
"Name": "Zhang San ",
"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 printing :");
Console. table (objGradeArr );
</Script>
</Body>
</Html>

Because I am not easy to understand English translation, You can go directly to the address after English for your reference.

Imagine you have created this list of programming versions and their file extensions:

The Code is as follows: Copy code

Var versions ages = [
{Name: "JavaScript", fileExtension: ". js "},
{Name: "TypeScript", fileExtension: ". ts "},
{Name: "CoffeeScript", fileExtension: ". coffee "}
];

Console. log (AGEs );

The console. log () call will 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 collapsed 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 (AGEs );
Make sure the console is opened before refreshing the page, otherwise you won't see any output. If you 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 undefined 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 () is that it also works with objects:

The Code is as follows: Copy code

Var versions ages = {
Csharp: {name: "C #", paradigm: "object-oriented "},
Fsharp: {name: "F #", paradigm: "functional "}
};

Console. table (AGEs );


'Nuff said.

Filtering the Displayed Object Properties
If you want to restrict the columns to certain properties, you can pass an array of their keys as a second parameter to the console. table () call:

The Code is as follows: Copy code

// Multiple property keys
Console. table (ages, ["name", "paradigm"]);
For a single property, a simple string is sufficient:

// A single property key
Console. table (ages, "name ");

If you do not understand on, can enter to see English: http://www.mariusschulz.com/2013/11/13/advanced-javascript-debugging-with-consoletable

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.