Table

From FirebugWiki

(Difference between revisions)
Jump to: navigation, search
(Added parameter description and examples)
m (Fixed source code of last example)
Line 40: Line 40:
}
}
-
<source lang="javascript">
 
var family = {};
var family = {};
family.mother = new Person("Susan", "Doyle", 32);
family.mother = new Person("Susan", "Doyle", 32);

Revision as of 12:51, 18 July 2012

Allows to log provided data using tabular layout. The method takes one required parameter that represents table-like data (array of arrays or list of objects). The optional columns parameter can be used to specify columns and/or properties to be logged (see more at softwareishard.com).

Contents

Syntax

table(data[, columns])

Parameters

data

Data to log.

This is either an object or an array. (required)

Columns

Array containing the names for the columns in the output table. (optional)

Examples

table([1, 2, 3])

This creates a table containing three rows. The first one will contain 1, the second 2 and the third 3. Because there was no column title defined, the column will be named Object Properties

>ou will get the same output via table({a: 1, b: 2, c: 3}).

table([[1, 2], [3, 4]])

This creates a table containing two rows with two columns. The first row will contain 1 and 2 and the second one 3 and 4. The columns will be named by their index, i.e. 0 and 1.

You will get the same output via table({a: [1, 2], b: [3, 4]}).

function Person(firstName, lastName, age)
{
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
}
 
var family = {};
family.mother = new Person("Susan", "Doyle", 32);
family.father = new Person("John", "Doyle", 33);
family.daughter = new Person("Lily", "Doyle", 5);
family.son = new Person("Mike", "Doyle", 8);
 
console.table(family);

This creates a table containing four rows with three columns. The columns contain the first name, the last name and the age of each person:

TableOutputFamilyExample.png

For more examples see the article at softwareishard.com.

See also

Personal tools