1

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.

4
  • How are you getting the string , I mean the data? Commented Feb 19, 2016 at 6:21
  • What you have there isn't valid JavaScript or JSON. Showing how you extracted that data from the chart might help someone answer. Commented Feb 19, 2016 at 6:22
  • if it is an array then just use data[0].keys() then you'll get array of keys. Commented Feb 19, 2016 at 6:22
  • I've just updated the question with the function that retrieves data from the chart. Commented Feb 19, 2016 at 6:23

1 Answer 1

6

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]);
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you Rejith. I did this - var chartData = getChartData("chartdiv"); var keysOnly = chartData[0].keys(); console.log(keysOnly); I get the error - TypeError: chartData[0].keys is not a function. Sounds like I'm missing including a library?
It's Object.keys not Object.prototype.keys. That would need to be called like Object.keys( data[0] ); . data[0].keys() will just throw an error because data[0].keys is `undefined|.
Cool. Thank you Paul & Rejith. Awesome stuff. Thank you tons.
@Paulpro I cant believe that I made such a silly mistake. Updated.
Cool. This helps. Thank you Rejith.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.