Breadcrumbs

Set Array Element Operator []

Set the value of an array element.

Syntax

set anArray[anIndex] = <expression>

Semantics

The operator [i] allows to set the i-th element of the array.

The first element has the position 0.

anIndex can be a static integer literal, an object type Integer or an action script language operation or expression returning an integer.

Substitutables

anArray

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

anIndex

Can be any expression that evaluates to an integer.

It is not possible to use class operations as index function – only built-in action script language operations are allowed.

Examples

Set a specific array element

Input: an array of names

JSON
{"names": ["Irene", "Eliza", "Raymond", "Jane", "David"]}
set names[1] = "Meredith";

Result:

JSON
{"names": ["Irene","Meredith", "Raymond", "Jane", "David"]}

Set the last array element

Input: an array of names

JSON
{"names": ["Irene", "Eliza", "Raymond", "Jane", "David"]}
set names[names.count()-1] = "Meredith";

Result:

JSON
{"names": ["Irene", "Eliza", "Raymond", "Jane", "Meredith"]}

Set a property from a complex array element

Input: a list of articles

JSON
{ "articles": [
    {"name": "AF-1200", "price": 64.50},
    {"name": "AF-1400", "price": 72.50},
    {"name": "CD-2002", "price": 570.00}
]}
set articles[0].price = 150.00;

Result:

JSON
{ "articles": [
    {"name": "AF-1200", "price": 150.00},
    {"name": "AF-1400", "price": 72.50},
    {"name": "CD-2002", "price": 570.00}
]}
📗

Related Pages: