📜  console.table javascript (1)

📅  最后修改于: 2023-12-03 14:40:12.197000             🧑  作者: Mango

Console.table in JavaScript

When working with data in JavaScript, it can be difficult to visualize what the data looks like. This is where console.table() comes in handy. It allows you to easily output tabular data in the console.

Basic Usage
const data = [
  { name: 'John', age: 25 },
  { name: 'Jane', age: 30 },
  { name: 'Jim', age: 35 }
];

console.table(data);

Output:

(index)  name  age
0        John  25
1        Jane  30
2        Jim   35
Customizing the Table

console.table() also allows you to customize how the data is displayed. You can specify which columns to display, as well as rename the column headers.

const data = [
  { first: 'John', last: 'Doe', age: 25 },
  { first: 'Jane', last: 'Doe', age: 30 },
  { first: 'Jim', last: 'Smith', age: 35 }
];

console.table(data, ['first', 'age'], ['First Name', 'Age']);

Output:

(index)  First Name  Age
0        John        25
1        Jane        30
2        Jim         35

console.table() can also be used to display nested data.

const data = [
  { name: 'John', age: 25, hobbies: ['Programming', 'Reading'] },
  { name: 'Jane', age: 30, hobbies: ['Gardening', 'Cooking'] },
  { name: 'Jim', age: 35, hobbies: ['Fishing', 'Hiking'] }
];

console.table(data);

Output:

(index)  name  age  hobbies
0        John  25   Array(2)
1        Jane  30   Array(2)
2        Jim   35   Array(2)

(index)  0          1
hobbies  Programming  Reading
hobbies  Gardening    Cooking
hobbies  Fishing      Hiking
Conclusion

console.table() is a useful tool in JavaScript for visualizing tabular data in the console. It allows for easy customization and can even display nested data.