Expressions can be used in Snaps such as the Mapper (Data) Snap to manipulate data. The expressions are based off a subset of JavaScript and act accordingly unless otherwise noted.
See Expression Language Usage for specifics on how to enter expressions.
Operators
Comparison Operators
Comparison operatorsArithmetic Operators | Description |
---|
Example |
---|
(+, -, *, /) |
Similar to the JavaScript arithmetic operators except that increment (++) and decrement (--) are not supported.
|
1/2 = .5 parseInt(1/2) = 0 |
Spread Operator |
Logical operators (&&, ||, !) behave the same as JavaScript logical operators.
String Operators
String operators (+) behave the same as JavaScript string operators except that the shorthand assignment operator (+=) is not supported.
Special Operators
Conditional (Ternary)
The conditional operator (?)(...) | This operator makes it easier to build arrays, objects, and call functions where the list of arguments is built at runtime. For instance, while using in an array literal, the value on the right of the operator is inserted into the new array at that position. Similarly, in a function call, the array expands and is used as the arguments to the function. In an object literal, the right side of the operator is another object that has its keys added to the new object. | To build a new array that is made up of another array surrounded by two elements:
To find the maximum value in an array of numbers:
To create a new object with some fixed values and entries in the "$extra" object:
| ||||||
Special Operators | ||||||||
Conditional (Ternary) (?) | This operator allows you to specify a value to use based on the result of an expression. |
Code Block |
---|
condition ? trueValue : falseValue |
condition
expression |
trueValue
will the trueValue will be the result of the expression if the condition evaluates to true, |
falseValue
will else the falseValue will be the result. |
Syntax: |
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
|
|
|
instanceof |
The |
Syntax: |
|
typeof
The typeof operator
| $my_array instanceof Array | |
The possible values for the type are: Null, Boolean, String, Number, Object, Array, Date, LocalDate, DateTime, and LocalDateTime. |
Example:
Code Block |
---|
$my_array instanceof Array |
match | This operator allows you to conditionally evaluate one of a number of expressions based on whether an input value matches a given pattern. Learn more about this operator, match Control Flow Operator. | match $score { 90..=100 => 'A', 80..<90 => 'B', 70..<80 => 'C', _ => 'F' } |
typeof | This operator returns the type of a value as a string. |
The possible return values are: "boolean", "number", "string", "object", and "array". Syntax: | typeof $name (Where $name="SnapLogic") returns "String" | |
in | This operator returns true if the property name or array index is in the given object or array. Syntax:
| 'name' in $ |
Comments
You can add notes to your expressions using comments.
A comment starts with '/*' and ends with '*/', for example:
Info |
---|
| ||
|
|
|
|
|
|
The comment
is ignored when evaluating the
expression—it is only for the reader's benefit.
Operator Precedence
Operator |
---|
Type | Individual |
---|
Operators | |
---|---|
member | . [] |
call | () |
negation | ! - |
multiply/divide | * / % |
addition/subtraction | + - |
relational | < <= > >= |
equality | == != |
logical-and | && |
logical-or | || |
comma | , |
Unsupported Operations
- Assignment
- Creating variables and assigning values is not supported
- Example: var temp = 10
- Short hand assignment
- Example: count += 5
- Strict equals
- Example: first === last
- Strict not equals
- Example: first !== last
- Increment
- Example: count++
- Decrement
- Example: count--
Accessing Document Values
To access values in a document,
you can use JavaScript object accessors
.
For a given document data:
Info |
---|
| ||
{ |
first_name: |
"James", |
|
"Smith" |
The expression $first_name would return the 'first_name' property which is James. |
You can also access the 'first_name' property |
by using array notation $['first_name'].
|
$[1] would return the value 2. |
Complex Example:
Info | ||
---|---|---|
| ||
{ |
|
"Bob", |
"Fred"] |
$names[2] would return the value Fred. |
Arrow Functions
You can create custom expression language functions
using the arrow function syntax:
Code Block |
---|
// A function that takes multiple parameters:
(param1, param2, ..., paramN) => expression
// A function that takes a single parameter does not need parentheses:
(param) => expression
param => expression
// A function with no parameters requires parentheses:
() => expression
// Function parameters can also have default values
(param1 = defaultValue1, param2 = defaultValue2, ..., paramN = defaultValueN) => expression |
These functions can be passed to other functions that accept callbacks, like Array.map() or Array.filter(), or they can be put into an expression library for use in any expression property in your
Pipelines.
Example
To multiply all numbers in an array by ten:
[1, 2, 3].map(x => x * 10)
Result:
[10, 20, 30]
Pages in this Section
Child pages (Children Display) |
---|
bgColor | #ebf7e1 |
---|---|
borderStyle | solid |