Mr. Javascript, can you tell me what’s in that [Object object], please?

I needed to find out the properties in an [Object object] I knew wasn’t null, but was giving me an exception:

The Solution

Searching the web, someone on stack overflow provided this handy-dandy routine to interrogate my JSON payload returned to JavaScript functions:

var output = '';
for (property in dat) {
 output += property + ': ' + dat[property]+'; ';
}

console.log(">> My Browser Console Logging << dat output: " + output);

And Voila!  Each property in the JSON document was spit out for me, line by line. Nice!

The Problem

Debugging someone else’s code today, I ran into this problem with this code snippet:

dat[0].viewId;
Browser Console error message says: uncaught exception viewId does not exist.

I knew the variable ‘dat‘ received a JSON payload from Chrome’s “network” tab of the Web Developer tools.

The Solution (Part II)

So I changed that code FROM:

dat[0].viewId;

TO:

dat[viewId];

And I was off the races again.

So the solution above came in handy.

Hopefully someone else out there will benefit from this bit of knowledge I gleaned.

Advertisements