1

I have an array named "dataset" where each entry is an object with a key and value attribute, such as the following:

dataset= 
 [
 {"key":"alpha", "value": [ {}, { } ...]},
 {"key":"beta", "value": [ { }, { } ...]},
 {"key":"gamma", "value": [ {}, { } ...]},
 {"key":"delta", "value": [ { }, { } ...]}
 ];
 dataset.domain:[];
 dataset.query:[];
 dataset.range:[];

I am working with D3.JS and I am using this dataset to plot in an stacked area chart the alpha, beta, gamma, and delta values. How can I use color.domain() to assign a different color to each "key"?

2
  • You cannot have the same key twice in an object. Is this really how your structure looks? Not some all these key/value pairs wrapped in an array? Commented Apr 3, 2014 at 21:50
  • @Thilo: I'm assuming that each key/value row should be wrapped in {}..., so the total structure is an array of objects, each of which contains a key name and a value sub-array. Commented Apr 3, 2014 at 22:00

1 Answer 1

2

To create an array that contains one property from each object in your data array, use an array mapping function:

dataset=  [
 {"key":"alpha", "value": [ {}, { } ...]},
 {"key":"beta", "value": [ { }, { } ...]},
 {"key":"gamma", "value": [ {}, { } ...]},
 {"key":"delta", "value": [ { }, { } ...]}
 ];

domain = dataset.map(function(o){return o.key});
/* domain is now ["alpha", "beta", "gamma", "delta"] */
Sign up to request clarification or add additional context in comments.

Comments

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.