20

So I have created a multi-dimensional array: (i.e. one with two sets of 'coordinates')

var items = [[1,2],[3,4],[5,6]];

My site is in development and the array, when it is loaded, and what it contains is constantly changing, so I need to be able to, whilst running the script, dynamically view the contents of the array.

So, I use this:

console.log(items);

to output it to the console. This gives me the following output:

alt: Unreadable output of my array

So, is there any other way in which I can do the equivalent of console.logging my array, but with more readable output?

Sam_12345
  • 249
  • 1
  • 3
  • 9

2 Answers2

55

You can use javascript's console.table

This will display your array in table form, making it much more readable and easy to analyse
It is quite a little known feature, however useful when you have a multi-dimentional array.

So, change your code to console.table(items);
It should give you something like this:

 -----------------------
|(index)|   0   |   1   |
|   0   |   1   |   2   |
|   1   |   3   |   4   |
|   2   |   5   |   6   |
 -----------------------
joe_young
  • 4,107
  • 2
  • 27
  • 38
30

You can use JSON.stringify()

console.log(JSON.stringify(items));

Its output will be like

[[1,2],[3,4],[5,6]]

var items = [[1,2],[3,4],[5,6]];
console.log(JSON.stringify(items));
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • 1
    Thank you this does provide a clearer output, I just prefer the table method. +1 for the nice method though :) – Sam_12345 May 31 '15 at 19:06