score:6
values
is an array. Looks like you want the first instance. Try this:
var eachPain = d3.values(data_group).map(function(d){ return d.values[0].Insect });
EDIT:
var eachPain = d3.values(data_group).map(function(d) {
return d.values.map(function(v) { return v.Insect; }).join(', ');
});
score:2
Just to add some more information, besides the one in the accepted answer:
If you want to get a specific insect, and if you know it's position in the array (as you seem to do), this is what you need:
var someInsect = data_group[i].values[j].insect;
//outer object ----^ ^---- inner object
In this variable, the index i
refers to the outer object, with all insects with a given level pain. Then, inside it, the index j
refers to the inner object, with a specific insect.
For instance, check this following demo (I'm using a <pre>
to load the data), where I get the data and nest it the way you did. In this demo, to get the tarantula hawk, I'm using:
var someInsect = data_group[0].values[1].insect;
var data = d3.csvParse(d3.select("#data").text());
var data_group = d3.nest()
.key(d => d.painIndex)
.entries(data);
var someInsect = data_group[0].values[1].insect;
console.log(someInsect);
pre {
display: none;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<pre id="data">insect,painIndex
fire ant,4
honey wasp,1
warrios wasp,3
paper wasp,1
velvet ant,2
tarantula hawk,4
bullet ant,4</pre>
Of course, that variable will change according to the insect you want.
Another possibility is getting the pain level of any insect you want by name. In your nested array, that can be done with:
var filtered = [].concat.apply([], data_group.map(function(d) {
return d.values
})).filter(function(d) {
return d.insect === myInsect
})[0].painIndex;
Where myInsect
holds the name of the insect you'll use in the filter
.
Check this other demo, where we get the pain value of the tarantula hawk:
var data = d3.csvParse(d3.select("#data").text());
var data_group = d3.nest()
.key(d => d.painIndex)
.entries(data);
var myInsect = "tarantula hawk"
var filtered = [].concat.apply([], data_group.map(function(d) {
return d.values
})).filter(function(d) {
return d.insect === myInsect
})[0].painIndex;
console.log(filtered);
pre {
display: none;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<pre id="data">insect,painIndex
fire ant,4
honey wasp,1
warrios wasp,3
paper wasp,1
velvet ant,2
tarantula hawk,4
bullet ant,4</pre>
Of course, all of this (getting the key values, get the value depending on other value etc...) would be way easier if you used your original array, instead of the nested one.
Source: stackoverflow.com
Related Query
- How do you retrieve the key values of a nested data set in D3
- How to retrieve the key values of a nested data set in D3
- How do I reformat the data in d3 having values to the key and another values to the value of the key?
- dc.js - how to get the average of a column in data set
- Display only values from the data set into X axis ticks
- When a d3.behavior.zoom event is triggered programatically, how do you set inital values for translate and scale?
- How to access nested data Values in D3
- With NVD3.js (nv.models.lineWithFocusChart), how do you set specific ticks on X-axis, when x values are dates?
- How do you call data from nested JSON array using d3?
- How to get the minimum and maximum value between a given range in data set in d3.js
- d3 library - how to set the legend values
- How do I set the X values for a singled stacked horizontal bar chart using d3.js?
- How do you add multiple lines to a D3JS line chart where the depedent variable data sources start at different positions along the range?
- How do you find the last data point in d3 and draw a circle and a line on it?
- How to rearrange the DOM order of svg elements based on their data values in a dynamically changing d3 vis?
- How can I retrieve the original data row to a dc.js boxplot data-point?
- How should I use map to get back some values of an object when the key are user input
- d3Js - how to arrange my values as around the 360d and automate data in a circle?
- How to set my variable so D3 data update finds the right object property?
- d3 line chart x, y not in the same level of a data structure, how to set x, y function?
- How to call all values in nested JSON data (d3js)
- How can I find the frequency of each member in my data set
- How can you access data that's nested like this in a csv file?
- How can I perform a d3 nested data-join where the child references data from the parent?
- How to set the background-color of a D3.js svg?
- How to find the max/min of a nested array in javascript?
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- How to update elements of D3 force layout when the underlying data changes
- In d3, how to get the interpolated line data from a SVG line?
- How do you remove the background gridlines in nvd3.js?
More Query from same tag
- Possible to use a circle pack layout in d3.js with fixed circle sizes?
- Creating clusters in a sortable bar chart in D3
- elasticY(true) with rangeChart does not adapt to new range
- Not able to disable tooltip nvD3.js
- How to select an element in svg in D3
- store and retrieve assigned index of svg elements
- d3.js change color and size on line graph dot on mouseover
- Plotting real-time/dynamic data with D3 js
- d3js: time scaling and winter months
- How to simulate mouse move in D3 so when you drag nodes, other nodes move automatically?
- D3 ticked function does not find d.target.x
- d3.js, multiple script tags blocking eachother
- Building JSON dynamically in D3 javascript
- how to use getElementById with getBBox to determine the svg width and height
- Bundle layout using d3.js and given json file structure
- Create circle on svg map
- Disable SVG drag
- How can I position text inside bubble chart?
- Setting points in a d3.js Global Map
- d3js tree.nodes() is not a function
- d3 version 4 issue with rectangle and text
- d3js Bars not appearing on Legend
- creating SVG text with javascript (in empty div)
- D3 Path with random colored lines
- web2py d3.json() where to put the json file?
- How to display a text when mouseover a cilcle
- Get coordinates of clicked on node in d3 tree and centre
- Add zoom to d3js map using d3fc-label-layout
- How do I get a tooltip on hovering over an SVG circle
- How to auto update sub elements datum binding in D3