console.log showing contents of array object
The console
object is available in Internet Explorer 8 or newer, but only if you open the Developer Tools window by pressing F12 or via the menu.
It stays available even if you close the Developer Tools window again until you close your IE.
Chorme and Opera always have an available console
, at least in the current versions. Firefox has a console
when using Firebug, but it may also provide one without Firebug.
In any case it is a save approach to make the use of console
output optional. Here are some examples on how to do that:
if (console) {
console.log('Hello World!');
}
if (console) console.debug('value of someVar: ' + someVar);
It's simple to print an object to console in Javascript. Just use the following syntax:
console.log( object );
or
console.log('object: %O', object );
A relatively unknown method is following which prints an object or array to the console as table:
console.table( object );
I think it is important to say that this kind of logging statement only works inside a browser environment. I used this with Google Chrome. You can watch the output of your console.log calls inside the Developer Console: Open it by right click on any element in the webpage and select 'Inspect'. Select tab 'Console'.
there are two potential simple solutions to dumping an array as string. Depending on the environment you're using:
…with modern browsers use JSON:
JSON.stringify(filters);
// returns this
"{"dvals":[{"brand":"1","count":"1"},{"brand":"2","count":"2"},{"brand":"3","count":"3"}]}"
…with something like node.js you can use console.info()
console.info(filters);
// will output:
{ dvals:
[ { brand: '1', count: '1' },
{ brand: '2', count: '2' },
{ brand: '3', count: '3' } ] }
Edit:
JSON.stringify comes with two more optional parameters. The third "spaces" parameter enables pretty printing:
JSON.stringify(
obj, // the object to stringify
replacer, // a function or array transforming the result
spaces // prettyprint indentation spaces
)
example:
JSON.stringify(filters, null, " ");
// returns this
"{
"dvals": [
{
"brand": "1",
"count": "1"
},
{
"brand": "2",
"count": "2"
},
{
"brand": "3",
"count": "3"
}
]
}"
console.log
does not produce any message box. I don't think it is available in any version of IE (nor Firefox) without the addition of firebug or some equivalent.
It is however available in Safari and Chrome. Since you mention Chrome I'll use that for my example.
You'll need to open your window and its developer window counterpart. you can do this by right clicking any element on the page and selecting "Inspect element". your window will be divided in two parts, the developer part being the bottom. in the division between the two parts is a bar with buttons and the rightmost button there is labeled "console". You'll need to click that to switch to the console tab. Press F12 for developer tools in most browsers on Windows, command + shift + I on macOS.
Once there, you will be able to interact with whatever page is loaded on top through javascript from that console, and any messages you console.log
will be displayed there.