Closed novotny1akub closed 2 years ago
I think you could simplify your code if you did your filter in one function:
cars_filtered = cars.filter(d => name_filter.indexOf(d.name) >= 0 && cyl_filter.indexOf(d.cylinder) >= 0)
I tried to change the code based on your suggestion, but it does not seem to be working. Is this what you had in mind? It does not return any table not only when selecting multiple values, but even when selecting single value in both filters.
d3 = require("d3@6")
// dummy data
cars = d3.csvParse(`name,cyl,mpg
car1,2,21.4
car2,4,18
car2,6,54
car3,2,18.6
`)
// filter for the car name
viewof name_filter = Inputs.select(
cars.map(d => d.name),
{value: [...new Set(cars.map(d => d.name))],
label: "Car Name", multiple: true, sort: true, unique: true}
)
// filter for the number of cylinders
viewof cyl_filter = Inputs.select(
cars.map(d => d.cyl),
{value: [...new Set(cars.map(d => d.cyl))],
label: "Number of Cylinders", multiple: true, sort: true, unique: true}
)
// applying the filters above
cars_filtered = cars.filter(d => name_filter.indexOf(d.name) >= 0 && cyl_filter.indexOf(d.cylinder) >= 0)
Inputs.table(cars_filtered)
I think it's because in your code it should be d.cyl
instead of d.cylinder
I've added this example to a notebook:
https://observablehq.com/d/97e99dde46096bf5#cell-17
I like your simplification a lot @enjalot thanks!
Is there any built-in way to filter an underlying dataset by mulltiple values coming from an input filter? Something like the
IN
clause in SQL. An example of what I mean by that below. The filtering is done using a custom functionin_array
, but I was just wondering if I am not reinventing the wheel.