How to use Array methods

This forum was archived to /woltlab and is now in read-only mode.
  • Can you help how to use method filter for arrays?

    From the documentation https://www.icinga.com/docs/ic…ry-reference/#arrayfilter im not sure how to enter syntax correctly.


    Example what not working, probably element is not correct identification of item in array :

    Code
    1. exampleArray = ["test1","test2","test3"]
    2. newArray = exampleArray.filter(element == "test2")
    3. result is newArray = ["test2"]

    Thank you

    The post was edited 1 time, last by idMaxCZ ().

  • filter() requires a lambda function which can be used as comparator. Yours is just a simple equal and won't have any effect.


    Code
    1. michi@mbmif ~/coding/icinga/icinga2 (feature/changelog-script *=) $ icinga2 console
    2. Icinga 2 (version: v2.6.3-387-g9d42b3fc0)
    3. <1> => arr = [ "foo1", "foo2", "abc" ]
    4. null
    5. <2> => new_arr = arr.filter(e => match("foo*", e))
    6. null
    7. <3> => new_arr
    8. [ "foo1", "foo2" ]