語法
js
console.table(data)
console.table(data, columns)
引數
data-
要顯示的資料。這必須是陣列或物件。陣列中的每個項或物件中的每個屬性都表示為表中的一行。表中的第一列標記為
(index),其值是陣列索引或屬性名。如果陣列中的元素或物件中的屬性本身是陣列或物件,則它們的項或屬性將在行中進行列舉,每列一個。
請注意,在 Firefox 中,
console.table()僅限於顯示 1000 行,包括標題行。 columns可選-
一個數組,可用於限制表中顯示的列。如果
data的每個條目都是陣列,則包含索引;如果data的每個條目都是物件,則包含屬性名。生成的表將僅包含與給定索引或名稱匹配的項的列。
返回值
無(undefined)。
示例
原始型別集合
data 引數可以是陣列或物件。
js
// an array of strings
console.table(["apples", "oranges", "bananas"]);
| (index) | 值 |
|---|---|
| 0 | 'apples' |
| 1 | 'oranges' |
| 2 | 'bananas' |
js
// an object whose properties are strings
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
const me = new Person("Tyrone", "Jones");
console.table(me);
| (index) | 值 |
|---|---|
| firstName | 'Tyrone' |
| lastName | 'Jones' |
複合型別集合
如果陣列中的元素或物件中的屬性本身是陣列或物件,則它們的元素或屬性將在行中進行列舉,每列一個。
js
// an array of arrays
const people = [
["Tyrone", "Jones"],
["Janet", "Smith"],
["Maria", "Cruz"],
];
console.table(people);
| (index) | 0 | 1 |
|---|---|---|
| 0 | 'Tyrone' | 'Jones' |
| 1 | 'Janet' | 'Smith' |
| 2 | 'Maria' | 'Cruz' |
js
// an array of objects
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
const tyrone = new Person("Tyrone", "Jones");
const janet = new Person("Janet", "Smith");
const maria = new Person("Maria", "Cruz");
console.table([tyrone, janet, maria]);
如果陣列包含物件,則列會用屬性名標記。
| (index) | firstName | lastName |
|---|---|---|
| 0 | 'Tyrone' | 'Jones' |
| 1 | 'Janet' | 'Smith' |
| 2 | 'Maria' | 'Cruz' |
js
// an object whose properties are objects
const family = {};
family.mother = new Person("Janet", "Jones");
family.father = new Person("Tyrone", "Jones");
family.daughter = new Person("Maria", "Jones");
console.table(family);
| (index) | firstName | lastName |
|---|---|---|
| daughter | 'Maria' | 'Jones' |
| father | 'Tyrone' | 'Jones' |
| mother | 'Janet' | 'Jones' |
限制顯示的列
預設情況下,console.table() 會列出每一行的所有元素。您可以使用可選的 columns 引數來選擇要顯示的一組列。
js
// an array of objects, logging only firstName
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
const tyrone = new Person("Tyrone", "Jones");
const janet = new Person("Janet", "Smith");
const maria = new Person("Maria", "Cruz");
console.table([tyrone, janet, maria], ["firstName"]);
| (index) | firstName |
|---|---|
| 0 | 'Tyrone' |
| 1 | 'Janet' |
| 2 | 'Maria' |
規範
| 規範 |
|---|
| 控制檯 # table |
瀏覽器相容性
載入中…