3👍
✅
You’re not doing bad, all you need now is to map that object into the data
and label
fields. Let’s begin with labels
Object.keys(dict)
returns an array of all the keys from dict
So we put
labels: Object.keys(dict)
Now we need the values for the data
array, with Object.keys
and map
we can do it easily, we use Object.keys
to get an array of all the keys in dict
and then map
to get each value from it’s key, like this:
data: Object.keys(dict).map(function (key) { return dict[key]; })
Both Object.keys
and map
are part of the ECMAScript 5 specification, so this wont work on ES3 (IE 8 or lower), but you can get pollyfils for both of those functions:
Source:stackexchange.com