Skip to content
Advertisement

D3 how to properly get the key value INSIDE of a json object

I am trying to generate my column headers based on the key value of the returned json object. However it is returning as [0,1] rather than [key[1],key[2]].

This is my json data and I’m trying to use D3 to get the key’s of this object (which is “Label”, “Count” for example) as my column headers rather than inserting it statically.

JavaScript

Here is my d3 code:

JavaScript

The tabulate function is where I am trying to get my key fields for the column headers but the code above seems to be getting the entire object instead of the value INSIDE. Example: [0,1] as column headers instead of [Label, Count] example:

Advertisement

Answer

Please note that data is an array of objects and not an object. So to get the keys of an object, you should apply d3.keys function on one of the objects in the array. Like this –

JavaScript

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement