score:0
the text field should map to this zone field in the array. In case of duplicate values, such as in this case, I want only 1 text element to be appended. Is the Array.filter function an option? If yes, then where do I use it?
Actually if you want to return only 1 object matching this zone
from your array, you can't use .filetr()
as it will return alll the elements that have the same zone
.
You can use Array#find()
it will return only the first object
that pass the condition:
The
find()
method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
This is how should be your code:
var myObj = dataVal.find(function(item) {
return item.zone === "Bandra-Pillers";
});
Demo:
var dataVal = [{
"count": 1267,
"path": 1,
"zone": "Bandra-Pillers"
},
{
"count": 697,
"path": 2,
"zone": "Bandra-Pillers"
},
{
"count": 560,
"path": 3,
"zone": "Bandra-Pillers"
}
];
var myObj = dataVal.find(function(item) {
return item.zone === "Bandra-Pillers";
});
console.log(myObj);
Edit:
You can call it in the callback function of .html()
like this:
var textG = g.append("g")
.append("text")
.html(function(d) {
return dataVal.filter(function(item){
return item.zone === zone; //Pass zone variable here
})[0].data.zone;
});
Source: stackoverflow.com
Related Query
- How to return only unique value from array objects bound to an element in d3.js
- How to get maximum value from an array of objects to use in d3.scale.linear().domain()
- How to get biggest array element based on his second sub array value
- How to get value from the element using selection in d3
- How to get the specific JSON array from a value in that array
- How do I access a datum's parent array from within a value function?
- Selecting a specific value from an array of objects in javascript
- How do I change color of path element when its value is selected from dropdown
- How to create an array of nested objects from a csv file in D3.js?
- D3 - How to create multiple line charts from an array of objects
- How to find min and max value in a array of objects
- D3.js how to extract Y domain value from data array with multiple Y axis columns
- How to get the data from given array of objects in d3.js
- How to return a value from a function, which is generated inside a callback function within?
- How to return a value from a promise which isn't a pending promise: d3.js, Javascript?
- D3.js : How can I select the first property value from an array of objects?
- derive array of unique property values from an array of objects
- D3.js x value scaling problem from Array of objects
- d3.js unique colors when plotting an arbitrary number of lines from an array of array of objects
- how to assign unique id to SVG text element in d3 javascript
- How to select unique values in d3.js from data
- how to get dynamic value of input element with d3?
- how can I exclude an element from an Angular scope?
- D3.js Selecting min/max value from an array of arrays (nesting d3.extent)
- How to load data from an internal JSON array rather than from an external resource / file for a collapsible tree in d3.js?
- How do you access an array of Objects using D3?
- d3.js - max and min value from json data which has array of values
- How can I use D3.js to take an array of Javascript objects use them in a line graph?
- In D3, how can I create multiple elements for each data element based on value in data?
- Javascript: Array of dictionaries, get all key value pairs from one dictionairy with condition
More Query from same tag
- D3 bar chart from objects with arrays
- removing d3 legend items failing
- Merge objects that share a common value in D3
- Create data visualizations using Express and get data from a PostgreSQL database
- d3 checking for types and setting a type accordingly
- Parameters needed for d3.mouse()
- Muliple pie charts are not loading
- D3 - How to update force simulation when data values change
- Multiple NVD3 charts on page. How to simplify the javascript code and wrap function?
- Not able to generate arc in d3,js for some values,
- d3.js filter from csv file using multiple columns
- Draw dashed outer line with D3
- D3 v4 Parallel Coordinate Plot Brush Selection
- Parse resource in d3.queue
- d3.groups by multiple keys given array of of keys
- D3 Treemap parent and children
- Understand promises in d3
- How to center a text on a svg image using d3.js?
- d3: why is variable set inside d3.json not accesible outside?
- how to animate the labels to flow with the donut chart with a start angle and end angle in d3.
- d3 chart refresh duplicate
- d3.js v4 - nodes are stuck in the top left corner
- D3 function to be called on mouse click (weird behaviour)
- d3 sankey diagram - how to set the y position
- D3 nesting - inconsistent number of levels
- Rollup with d3 plugin : reference is not defined
- How can I use 2 range sliders at the same time?
- D3.csvParse and D3.csv(filename) not working in Angular
- problem with D3 lasso circle element with text
- Building JSON dynamically in D3 javascript