Reputation: 5148
I would like to select elements in my data array that have a specific paramter value.
ex:
[{type: "suv", value:10},{type: "suv", value:20},{type: "berline", value:15},{type: "berline", value:5}]
So, how would I only have the type:"suv"
to be taken into account when plotting my value data?
The only similar thing I could find is selectAll, but it seems to only select elements in the UI (?)
Bear with me if this is simpler, I'm not too used to d3.js, and the information on the subject is rare.
Upvotes: 0
Views: 2377
Reputation: 707356
You can loop through the array with your own filter function like this returning a new array with only the elements in it that match the filter:
var filterArray(arr, key, value) {
var result = [], item;
for (var i = 0; i < arr.length; i++) {
item = arr[i];
if (item[key] && item[key] === value) {
result.push(item);
}
}
return(result);
}
var myArr = [{type: "suv", value:10},{type: "suv", value:20},{type: "berline", value:15},{type: "berline", value:5}];
var filteredArr = filterArray(myArr, "type", "suv");
Upvotes: 0
Reputation: 15366
If you're trying to filter your data before using d3.selectAll(...).data(...)...
, then use this:
var myArr = [{type: "suv", value:10},{type: "suv", value:20},{type: "berline", value:15},{type: "berline", value:5}];
var myFilteredArr = myArr.filter(function(d) { return d.type == "suv"; });
Are you instead trying to modify what's already displayed?
Upvotes: 5