console.table(data)
logs your data as a table in the console
console.table(data)
can be used to log any array or enumerable properties of an object into a table format in the console.
This function is available in all modern browsers, except IE.
The function takes one mandatory argument, the data
that you want to log. It must be an object or array. It has a second optional parameter which can define which columns
that you want to display.
The first column in the table will be labeled (index)
. If data
is an array, then its values will be the array indices. If data is an object, then its values will be the property names.
// logs a sortable table right in the console!
console.table(["one", "two", "three"]);
(index) |
Values |
0 |
“one” |
1 |
“two” |
2 |
“three” |
Object with string properties
const person = {
firstName: "RJ",
lastName: "Schmertz"
}
console.table(person);
(index) |
Values |
firstName |
“RJ” |
lastName |
“Schmertz” |
Array of objects
const person1 = {
firstName: "RJ",
lastName: "Schmertz"
}
const person2 = {
firstName: "Ryan",
lastName: "Smith"
}
const person3 = {
firstName: "Cassie",
lastName: "Jones"
}
console.table([person1, person2, person3]);
(index) |
firstName |
lastName |
0 |
“RJ” |
“Schmertz” |
1 |
“Ryan” |
“Smith” |
2 |
“Cassie” |
“Jones” |
Object whose properties are objects
const person1 = {
firstName: "RJ",
lastName: "Schmertz"
}
const person2 = {
firstName: "Ryan",
lastName: "Smith"
}
const person3 = {
firstName: "Cassie",
lastName: "Jones"
}
const family = {};
family.father = person1;
family.brother = person2;
family.sister = person3;
console.table(family);
(index) |
firstName |
lastName |
father |
“RJ” |
“Schmertz” |
brother |
“Ryan” |
“Smith” |
sister |
“Cassie” |
“Jones” |
Restricting which columns are displayed
const person1 = {
firstName: "RJ",
lastName: "Schmertz"
}
const person2 = {
firstName: "Ryan",
lastName: "Smith"
}
const person3 = {
firstName: "Cassie",
lastName: "Jones"
}
const family = {};
family.father = person1;
family.brother = person2;
family.sister = person3;
console.table([person1, person2, person3], ["firstName"]);
(index) |
firstName |
0 |
“RJ” |
1 |
“Ryan” |
2 |
“Cassie” |