11-17-2017 12:00 AM
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?
11-17-2017 02:44 AM
What are these OBJ1 … OBJ8 etc?
Are you more looking to have dynamically evaluate the number of ternary operations here?
11-17-2017 03:47 AM
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')
}
11-17-2017 04:37 AM
you can also consider using the conditional snap which would make this easier to maintain / understand.