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:
So, is there any other way in which I can do the equivalent of console.logging
my array, but with more readable output?
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.
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.
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.
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 | -----------------------
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With