usert4jju7
usert4jju7

Reputation: 1813

JSON / JavaScript get object keys

I'm extracting data from as follows -

The above function returns data in the below format -

[Object { date=Date,  value=112,  volume=1469}, Object { date=Date,  value=124,  volume=539}, Object { date=Date,  value=114,  volume=859}, Object { date=Date,  value=123,  volume=1284}, Object { date=Date,  value=113,  volume=1382}, Object { date=Date,  value=129,  volume=1353}]

I would like to obtain the list of keys only as a simple array.(Parsing the first object in the array is enough to get this as all other objects have the same keys) In case of the above output, I would like the simple array to look as ["date","value","volume"]

I tried JSON.stringify & then parse but it still doesn't work.

Also, how could I convert the whole array obtained from the chart into a simple array please?

I'm visualizing an output of the below type -

[{'date':'whatever',  'value':126,  'volume':911},
{'date':'whatever',  'value':136,  'volume':1005},
{'date':'whatever',  'value':125,  'volume':720}]

If the question doesn't make sense, please let me know. I'll see how best I could re-word.

Upvotes: 1

Views: 276

Answers (1)

rrk
rrk

Reputation: 15846

Use Object.keys() function. Here in the response from function you get an array. Use that to get the list of keys.

var data = getChartData('param')
Object.keys(data[0]);

Upvotes: 6

Related Questions