# Flux query: Problem with array filter inside map

**URL:** <https://community.influxdata.com/t/flux-query-problem-with-array-filter-inside-map/25101>\
**Category:** InfluxDB 2\
**Tags:** query\
**Created:** [May 19, 2022, 9:38am UTC](https://community.influxdata.com/t/flux-query-problem-with-array-filter-inside-map/25101 "2022-05-19T09:38:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Solmis](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/solmis/32/10194_2.png) [@Solmis](https://community.influxdata.com/u/Solmis)\
**Post date:** [May 19, 2022, 9:38am UTC](https://community.influxdata.com/t/flux-query-problem-with-array-filter-inside-map/25101/1 "2022-05-19T09:38:51Z")

</div>

Hi,

I’ve encountered a problem with a Flux query and I’m unable to make it work. Here’s the query that WORKS:

```auto
import "strings"
import "experimental/array"

items_with_id = ["Apple (2)", "Orange (200)"]
item_ids = array.map(arr: items_with_id, fn: (x) => strings.substring(v: x, start: strings.index(v: x, substr: "(") + 1,
     end: strings.index(v: x, substr: ")")))
rr = array.filter(arr: items_with_id, fn: (x) => strings.containsStr(v: x, substr: "(" + "2" + ")"))[0]

from(bucket: "my_bucket")
  |> range(start: -10d)
  |> filter(fn: (r) =>
    r._measurement == "fruit" and
    r._field == "val" and
    contains(set: item_ids, value: r.item_id)
  )
  |> group(columns: ["_measurement", "item_id", "_time"])
  |> sum()
  |> group(columns: ["item_id"])
  |> aggregateWindow(every: 1h, fn: sum, createEmpty: true)
  |> drop(columns: ["_measurement", "_field", "name"])
  |> map(fn: (r) => ({ r with item_id: rr }))
  |> yield()

```

but when I replace the **map** line before **yield()** with expression behind **rr** :

```auto
  |> map(fn: (r) => ({ r with item_id: array.filter(arr: items_with_id, fn: (x) => (strings.containsStr(v: x, substr: "(" + "2" + ")")))[0] }))

```

Then I get error:

```auto
error calling function "map" @20:6-20:150: name {"x" ""} does not exist in scope

```

Finally I would like to replace `substr: "(" + "2" + ")"` with `substr: "(" + r.item_id + ")"`. What am I doing wrong? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![yoursaaron](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/yoursaaron/32/12819_2.png) [@yoursaaron](https://community.influxdata.com/u/yoursaaron)\
**Post date:** [April 26, 2023, 2:23am UTC](https://community.influxdata.com/t/flux-query-problem-with-array-filter-inside-map/25101/2 "2023-04-26T02:23:46Z")

</div>

i have the same problem
