Skip to main content
Skip table of contents

apply Statement

Applies an Action Script operation or custom class operation to each element of an array.

Syntax

apply operationName(element [, additionalParameter]) to targetArrayName
apply element.operationName([parameter]) to targetArrayName

Semantics

Applies an Action Acript operation or class operation (operationName) to each element of an array (targetArrayName).

The array elements can be of simple or complex type. element is a keyword and denotes any single element of the target array (targetArrayName).

Substitutables

operationName

Can be any action script operation or custom class operation. The operation must return an object of the array type.

targetArrayName

Can be any variable or object attribute having the type Array.

Examples

Concatenate a string to each array element

Input: an array of article names

JSON
{"articles": ["AF-1200", "AF-1400", "CD-2002", "RC-0001"]}
CODE
set result = apply concat(element, "_new") to articles;

Result:

JSON
{"result": ["AF-1200_new", "AF-1400_new", "CD-2002_new", "RC-0001_new"]}

Normalize spaces of each array element

Input: an array of article names

JSON
{"articles": ["  AF-1200", "AF-1400  ", "  CD-2002  ", "RC   0001", "K-6"]}
CODE
set result = apply normalizeSpaces(element) to articles;

Result:

JSON
{"result": ["AF-1200", "AF-1400", "CD-2002", "RC 0001", "K-6"]}

Use apply on Complex Arrays

To use apply on arrays containing complex objects, you need to implement a custom operation that returns a changed object. The example below shows how to perform a calculation on the price property of a complex article object.

Input: an array of a complex article objects

JSON
{ "articles": [
    {"name": "AF-1200", "price": 64.50},
    {"name": "AF-1400", "price": 72.50},
    {"name": "CD-2002", "price": 570.00}
]}
CODE
set result = apply element.raisePrice(1.1) to articles;

raisePrice() in this example is a custom operation that takes an article object, does the calculations, and returns a modified article object.

Result:

JSON
{ "articles": [
    {"name": "AF-1200", "price": 70.95},
    {"name": "AF-1400", "price": 79.75},
    {"name": "CD-2002", "price": 627.00}
]}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.