0

Given the following:

LET replacements = [
  ["foo", "bar"],
  ["bar", "baz"]
]

LET title = "foo"

// JS CODE
// title = replacements.reduce((acc, r) => r.replace(acc[0], acc[1]), title);
// or
// for (const r of replacements) {
//   title = title.replace(r[0], r[1]);
// }

RETURN title

How is the logic I described with JS possible to implement in aql?

I can't seem to get FOR loops to work without returning something, and LET itself seems not to allow further reassignment.

3
  • 1
    What is your expected result? You want to return the value 'bar' in this case? Commented Oct 26, 2022 at 8:18
  • The expected result is 'baz', as you'd get after the replacements implemented in JS had been executed Commented Oct 26, 2022 at 10:39
  • 1
    I don't think it's possible to do in pure AQL, you would use Foxx Microservices if you wanted to solve this. LET assignments cannot be changed once made. Commented Oct 26, 2022 at 12:25

1 Answer 1

1

This could be a case for a user function.

In arangosh:

127.0.0.1:8529@_system> require("@arangodb/aql/functions").register(
    "MYFUNC::REPLACEEQ", 
    function (replacements, title) {
        return replacements.reduce(
                (t, r) => t.replace(r[0], r[1]),
                title
        );
    }
);

The AQL-Query:

LET replacements = [
  ["foo", "bar"],
  ["bar", "baz"]
]

RETURN MYFUNC::REPLACEEQ(replacements, "foo")
// => ["baz"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.