reduce Statement
Reduces an array to a scalar value by recursively applying an expression to each array element and its next element.
Syntax  | 
  | |
|---|---|---|
Semantics  | 
 
 
 The strength of  String operations, however, are not very efficient, and may cause performance issues with big arrays.  | |
Substitutables  | 
  | Can be any variable or object attribute having the type Array.  | 
  | 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.  | |
  | The expression after   | |
Error Codes  | 
  | The reduce function can only work on complex types.  | 
  | The first item of a reduce function is NULL.  | |
  | 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
     
    
        CODE
     
    
Result: result is “Irene Eliza Raymond Jane David".  | |
Get the overall length of all strings in an array Input: an array of names 
        JSON
     
    
        CODE
     
    
Result: result is 26.  | ||
Calculate a sum through all array elements Input: an array of a complex article 
        JSON
     
    
        CODE
     
    
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.
{ "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}
]}
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.
{"articles": [
    {"name": "AF-1200", "category": "Adapter", "serviceInterval": 52}
]}
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.
{"numbers": [1,2,3]}
set result = reduce numbers using element + nextElement
                            if single use element;
:1_one_square_blue: First iteration
The first occurrence of the
elementexpression is initialized (here: take the first integer 1).Then the next element is evaluated and added (
+ nextElement, 2 in this example).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.
nextElementis added again, which is 3 in this example.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.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.