0

I have variable filter that holds a string that represents a function:

const filter = `equalsFilter('elements.url_pattern', ${urlPattern})`

I want to chain it on another object like this:

query.filter

of course, this doesn't work because the filter is a string. Essentially I'm aiming for this result:

query.equalsFilter('elements.url_pattern', urlPattern)

How can I achieve somewhat like described above?

karolis2017
  • 2,195
  • 8
  • 24
  • 49
  • Possible duplicate of [Is there a way to create a function from a string with javascript?](https://stackoverflow.com/questions/7650071/is-there-a-way-to-create-a-function-from-a-string-with-javascript) – Marek Koziorowski Jul 27 '19 at 11:59
  • Why would you have a string of code, where does the string come from? Also, what is `urlPattern`, is it a string literal? – Bergi Jul 27 '19 at 13:02

1 Answers1

0

Solved this by passing a function representation as an object

const filter = {
      type: 'equalsFilter',
      element: 'elements.url_pattern',
      value: urlPattern
    }

Then

query[filter.type](filter.element, filter.value)
karolis2017
  • 2,195
  • 8
  • 24
  • 49