Forum Discussion

krupalibshah's avatar
krupalibshah
Contributor
8 years ago
Solved

Using 'in' clause in a mapper

I am trying to use ‘in’ clause in a mapper snap. But it does not evaluate correctly.

30 in [30,40,50] ----> false

30 in [‘30’,‘4’,‘50’] ----> false

(x => x in [‘4’,‘5’,‘6’])(4) ----> false

Has anyone tried it already? Am I missing anything?

  • Syntax for checking if a value is in an array is the same as in javascript.

    Try:

    [30,40,50].indexOf(30) != -1 —> true

    [30,40,50].indexOf(90) != -1 —> false

    The reason for the “!= -1” is that indexOf returns -1 if the element is not found in an array. Otherwise it returns the index.

    Same method works for strings.

5 Replies

  • roi6rieK's avatar
    roi6rieK
    New Contributor

    Syntax for checking if a value is in an array is the same as in javascript.

    Try:

    [30,40,50].indexOf(30) != -1 —> true

    [30,40,50].indexOf(90) != -1 —> false

    The reason for the “!= -1” is that indexOf returns -1 if the element is not found in an array. Otherwise it returns the index.

    Same method works for strings.

  • dmiller's avatar
    dmiller
    Former Employee

    Verified with Dev that “in” tests for index, not value, so 30 in [30,40,50] will be false, but 2 in [30,40,50] will be true.

    • krupalibshah's avatar
      krupalibshah
      Contributor

      Thanks for the help.

      However I think it should search for values #enhancement-requests