Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.log a multi-dimensional array

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?

like image 720
Sam_12345 Avatar asked May 31 '15 18:05

Sam_12345


People also ask

How do I access nested array?

To access an element of the multidimensional array, you first use square brackets to access an element of the outer array that returns an inner array; and then use another square bracket to access the element of the inner array.

What is multi dimensional array?

A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns. Each element is defined by two subscripts, the row index and the column index.

What is an example of a multidimensional array?

The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int x[10][20] can store total (10*20) = 200 elements. Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.


2 Answers

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   |
 -----------------------
like image 139
joe_young Avatar answered Oct 09 '22 10:10

joe_young


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));
like image 27
Satpal Avatar answered Oct 09 '22 09:10

Satpal