Reputation: 3752
I use d3's scaleLinear. I want to filter values that are outside of the scale's domain.
var arr = [0, 1, 15, 60, 700];
var scaleX = d3.scaleLinear()
.domain([10, 100])
.range([0, 200]);
// I need something like this:
scaleX.filter(arr) // I want to get: [1, 15, 60]
My best solution is foreach all the values and check that the domain's min/max is less/greater than the given value. Like this.
Is there any more compact solution?
Upvotes: 0
Views: 775
Reputation: 81
Apply the filter method to the arr
variable:
const filteredArray = arr.filter(function (value) { return ((value >= 10) && (value <= 100)) });
The filteredArray
will contain only the values between 10 and 100 (inclusive).
You can read more about the .filter
method here.
Upvotes: 1
Reputation: 3412
var filteredArr = arr.filter(function(d) {
return d >= scaleX.domain()[0] && d <= scaleX.domain()[1]
})
Upvotes: 1