Forum Discussion

shubhisharma's avatar
shubhisharma
New Contributor
8 years ago

Expression Library

Hello All,

If I have an expression library, having content:

{
FindObjectID:x => (
(x == ‘OBJ1’ ? ‘OBJ2’ :
(x == ‘OBJ3’ ? ‘OBJ4’ :
(x == ‘OBJ5’ ? ‘OBJ6’ :
(x == ‘OBJ7’ ? ‘OBJ8’ :
(x == ‘OBJ9’ ? ‘OBJ10’ :
(x == ‘OBJ11’ ? ‘OBJ12’ :
(x == ‘OBJ13’ ? ‘OBJ14’ :
(x == ‘OBJ15’ ? ‘OBJ16’ :
(x == ‘OBJ17’ ? ‘OBJ18’ :
‘UNKNOWN’))))))))))
}

Is there any way to simplify this huge expression?

3 Replies

  • What are these OBJ1 … OBJ8 etc?

    Are you more looking to have dynamically evaluate the number of ternary operations here?

  • tstack's avatar
    tstack
    Former Employee

    Since your conditions are simple string comparisons, I would suggest using an Object Literal to store the values for each key and then using the get() method to do the lookup:

    {
        objs: {
            OBJ1: 'OBJ2',
            OBJ3: 'OBJ4',
            OBJ5: 'OBJ6',
            OBJ7: 'OBJ8',
        },
        FindObjectID: x => this.objs.get(x, 'UNKNOWN')
    }
  • aleung's avatar
    aleung
    Contributor III

    you can also consider using the conditional snap which would make this easier to maintain / understand.