Without using any toolkit/lib/framework of any sort, this already is (should be) a pretty easy task.
First, please do note that JS doesn't have associative arrays, it has arrays (which are, in essence, augmented objects) and objects. You are dealing with the latter.
Here's how I'd do it:
var data = yourData[0],//your data is an array, containing a single object literal
header = [];
tbl = {},
p, p2;
for(p in data)
{
if (data.hasOwnProperty(p))
{
for (p2 in data[p])
{
if (data[p].hasOwnProperty(p2))
{
if (!tbl.hasOwnProperty(p2))
{
header.push(p2);//add header value
tbl[p2] = [];//create array
}
tbl[p2].push(data[p][p2]);
}
}
}
}
Then, you have the header
array that lists all keys available to you, and in the tbl
object, you find an array of values that go with each key. It should be pretty easy to write a loop that prints out just what you need to print out.
Alternatively, you can use the header
array and generate the output rows "real-time":
var data = yourData[0],//your data is an array, containing a single object literal
header,//leave undefined
row = [],
tbl = [],
p, i, p2;
for(p in data)
{
if (data.hasOwnProperty(p))
{
if (header === undefined)
{
header = [];
for (p2 in data[p])
header.push(p2);
tbl.push(header.join("\t"));
}
for (i=0;i<header.length;++i)
row.push(data[p][header[i]]);
tbl.push(row.join("\t"));
row = [];//re-initialize
}
}
console.log(tbl.join("\n"));//should be pretty accurate
Now, this will work an all browsers I know, including those crummy old IE's that some people insist on using. If you don't care much for these browsers (I don't, anyway), you can easily write this:
var data = yourData[0],//your data is an array, containing a single object literal
header;//leave undefined
str = '';//output string
p, i;
for(p in data)
{
if (header === undefined)
{//first time over
header = Object.getOwnPropertyNames(data[p]);
str = header.join("\t") + "\n";//stringify, tab separated, add new line
}
for (i=0;i<header.length;++i)
str += data[p][header[i]] + "\t";//add value
str += "\n";
}
console.log(str);//should be close to what you need
Disclaimer
I've written this code off the top of my head, none of it has been tested, so you may still have some debugging work ahead of you. All three of these snippets are, IMO, pretty self-explanatory, but feel free to ask for more details on a given statement if it isn't quite clear.
Note:
After adding this disclaimer, I did in fact test the last snippet I gave here in my console, the result I got was:
id firstname companyid companyname
gjr one ff05t5 GMC
68g two ff05t5 GMC
ghj three a23atr Ford
68f four ff05t5 GMC
f78 five gjd94d Jaguar
fh5 six gjd94d Jaguar
Which, formatting asside seems to be what you're after