Skip to main content
Skip table of contents

reduce Statement

Reduces an array to a scalar value by recursively applying an expression to each array element and its next element.

Syntax

reduce anArray using <expression with element and nextElement> if single use <expression with element>

Semantics

reduce allows you to reduce an array having elements of base or complex types to a scalar value by recursively applying an expression to each array element and its next element. The array must exists and contain at least one element.

element and nextElement are keywords and allow you to use relative references (as opposed to normal, absolute indexes) to single elements of an array, see Get Array Element Operator []).

if single use is a mandatory extension and allows to define the action for an array containing only one single element.

The strength of reduce is to efficiently perform calculations on array elements.

String operations, however, are not very efficient, and may cause performance issues with big arrays.

Substitutables

anArray

Can be any variable or object attribute having the type Array

<expression with element and next element>

This expression must use the currently evaluated array element and its next neighbor and evaluates to a scalar having the same type as the result value.

<expression with element>

The expression after if single use uses the sole array element and evaluates to a scalar having the same type as the result value.

Error Codes

FUMSM/9

The reduce function can only work on complex types.

FUMSM/10

The first item of a reduce function is NULL.

FUMSM/11

The reduce function returned NULL.

Examples

Concatenate all string elements of an array to a single string, separated by space

Input: an array of names

JSON
{"names": ["Irene", "Eliza", "Raymond", "Jane", "David"]}
CODE
set result = reduce names using concat(element, " ", nextElement)
                          if single use element;

Result: result is “Irene Eliza Raymond Jane David".

Get the overall length of all strings in an array

Input: an array of names

JSON
{"names": ["Irene", "Eliza", "Raymond", "Jane", "David"]}
CODE
set result = reduce names using element.stringLength() + nextElement.stringLength()
                          if single use element.stringLength();

Result: result is 26.

Calculate a sum through all array elements

Input: an array of a complex article

JSON
{ "articles": [
    {"name": "AF-1200", "category": "Adapter", "orderValue": 10,256.30},
    {"name": "CD-2024", "category": "Connector", "orderValue": 9,563.78},
    {"name": "RC-0002", "category": "Adapter", "orderValue": 162.55},
    {"name": "CD-2002", "category": "Connector", "orderValue": 510.01}
]}
CODE
set result = reduce articles using element.orderValue + nextElement.orderValue 
                             if single use element.orderValue;        

Result: result is 20,492.64.

ActionScript_ArrayOperations_Example

Click here to download a simple example model that shows how to use Action Script to handle arrays with Scheer PAS Designer.

Some More Examples

Reducing Arrays with Elements of Complex Type

The same algorithm applies to arrays with elements of complex types as described above. Since each element or nextElement refers to an object of complex type, you can access the attributes of the object like any other instance (e.g. element.name).

Input: an array of a complex article

In the example below, array articles contains objects of complex type Article. You want to copy the name property of each array element to a single string separated by a comma.

JSON
{ "articles": [
    {"name": "AF-1200", "category": "Adapter", "serviceInterval": 52},
    {"name": "CD-2024", "category": "Connector", "serviceInterval": 208},
    {"name": "RC-0002", "category": "Adapter", "serviceInterval": 104},
    {"name": "CD-2002", "category": "Connector", "serviceInterval": 26}
]}
CODE
set result = reduce articles using concat(element.name, ", ", nextElement.name)
                    if single use element.name; 

Result: result is “AF-1200, CD-2024, RC-0002, CD-2002”.

Single Array Element

For arrays that contain only one single element, you can use the (mandatory) extension if single use in the reduce statement.

Input: an array of a complex article

Let’s enhance the previous example with a text.

JSON
{"articles": [
    {"name": "AF-1200", "category": "Adapter", "serviceInterval": 52}
]}
CODE
set result = reduce articles using concat(element.name, ", ", nextElement.name)
                    if single use concat("Single article: ", element.name);

Result: result is “Single article: AF-1200”.

If an array has only one single element, the statement following if single use will be evaluated.

Reduce Algorithm

The reduce algorithm works as follows: Suppose you want to sum up all elements of an array numbers containing the numbers 1, 2, and 3.

JSON
{"numbers": [1,2,3]}
CODE
set result = reduce numbers using element + nextElement
                            if single use element;

:1_one_square_blue: First iteration

  1. The first occurrence of the element expression is initialized (here: take the first integer 1).

  2. Then the next element is evaluated and added (+ nextElement, 2 in this example).

  3. The result of the first iteration is 3.

:2_two_square_blue: Second iteration
Now, element refers to the intermediate result of the first iteration.

  1. nextElement is added again, which is 3 in this example.

  2. The resulting value is 6.

:3_three_square_blue: FinalizationThe final value is finally assigned to integer result.

ActionScript_ArrayOperations_Example

Click here to download a simple example model that shows how to use Action Script to handle arrays with Scheer PAS Designer.

Related Content

Related Pages:

  • apply Statement

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

  • reduce Statement

    Reduces an array to a scalar value by recursively applying an expression to each array element and its next element.

  • sort Statement

    Sort an array by using an expression to compare array elements with each other.

  • sort Statement
    Sort an array by using an expression to compare array elements with each other.

JavaScript errors detected

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

If this problem persists, please contact our support.