Flux query: Problem with array filter inside map

Hi,

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

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:

  |> 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:

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.

i have the same problem