# Conditional filtering with Inputs.select

**URL:** <https://talk.observablehq.com/t/conditional-filtering-with-inputs-select/8280>\
**Category:** Help\
**Created:** [September 11, 2023, 6:56pm UTC](https://talk.observablehq.com/t/conditional-filtering-with-inputs-select/8280 "2023-09-11T18:56:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![boshek](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@boshek](https://talk.observablehq.com/u/boshek)\
**Post date:** [September 11, 2023, 6:56pm UTC](https://talk.observablehq.com/t/conditional-filtering-with-inputs-select/8280/1 "2023-09-11T18:56:59Z")

</div>

I am trying to learn a good way to provide a custom value for `Inputs.select` (in this case “All”) and then subset the number of rows to plot based on that. Using the penguins dataset that it straightforward if I am using levels of a variable that are in the data already. But for something like “All”, I really only know how to do a conditional like this:

```js
pplot = {
  let pplot;
  if (pen_types == "All") {
    return pplot = penguins;
  } else {
    return pplot = penguins.filter(aq.escape(d => op.includes(d.species, pen_types)));
  }
}

```

So this is saying "if `pen_types` is “All” I want the whole dataset, otherwise subset the rows based on the value of `pen_types`. This seems verbose to me. For example is there any way within `filter` that I can just say “if ‘All’ give me all the rows” or something like this? Notebook here:

> **[NULL dropdown values](https://observablehq.com/d/60dc128e129abd86)**

---

<div class="post-metadata">

**Author:** ![boshek](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@boshek](https://talk.observablehq.com/u/boshek)\
**Post date:** [September 11, 2023, 8:23pm UTC](https://talk.observablehq.com/t/conditional-filtering-with-inputs-select/8280/2 "2023-09-11T20:23:52Z")

</div>

Oh maybe the ternary operator is what I need:

```js
 foo = {
  return (pen_types === "All")
  ? penguins // If pen_types is "All", no filtering is applied
  : penguins.filter(aq.escape(d => op.includes(d.species, pen_types)));
 }

```

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [September 12, 2023, 8:10am UTC](https://talk.observablehq.com/t/conditional-filtering-with-inputs-select/8280/3 "2023-09-12T08:10:49Z")

</div>

You can also filter directly in Plot which will preserve the original domains:

```js
    Plot.dot(penguins, {
      y: "body_mass_g",
      x: "bill_length_mm",
      fill: "species",
      filter: pen_types === "All" ? true : d => d.species === pen_types
    })

```

---

<div class="post-metadata">

**Author:** ![boshek](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@boshek](https://talk.observablehq.com/u/boshek)\
**Post date:** [September 12, 2023, 6:06pm UTC](https://talk.observablehq.com/t/conditional-filtering-with-inputs-select/8280/4 "2023-09-12T18:06:06Z")

</div>

Ah yes I always forget about that. In my case I have multiple dropdowns and multiple filtering steps so I think it is easier to separate out (i think?).

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [September 12, 2023, 9:06pm UTC](https://talk.observablehq.com/t/conditional-filtering-with-inputs-select/8280/5 "2023-09-12T21:06:03Z")

</div>

Difficult to tell without a concrete example. You can combine filters by using a pattern like

```auto
filters = [
  // changing the filter callback on the fly
  pen_types === "All"
    ? () => true
    : d => d.species === pen_types,
  // creating a Set in a closure to avoid Array.includes()
  (set => d => set.has(d.island))(new Set([selected_islands])),
  // a basic filter callback
  d => d.bill_length_mm >= min_length
]

```

```auto
    Plot.dot(penguins, {
      y: "body_mass_g",
      x: "bill_length_mm",
      fill: "species",
      filter: d => filters.every(func => func(d))
    })

```
