|
| 1 | +--- |
| 2 | +description: Reference for the 'lastIndexOf' DSC configuration document function |
| 3 | +ms.date: 08/29/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: lastIndexOf |
| 6 | +--- |
| 7 | + |
| 8 | +## Synopsis |
| 9 | + |
| 10 | +Returns an integer for the index of the last occurrence of an item in an array. |
| 11 | +If the item isn't present, returns -1. |
| 12 | + |
| 13 | +## Syntax |
| 14 | + |
| 15 | +```Syntax |
| 16 | +lastIndexOf(arrayToSearch, itemToFind) |
| 17 | +``` |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +The `lastIndexOf()` function searches an array from the end to the beginning |
| 22 | +and returns the zero-based index of the last matching element. String |
| 23 | +comparisons are case-sensitive. If no match is found, `-1` is returned. |
| 24 | + |
| 25 | +Supported `itemToFind` types: |
| 26 | + |
| 27 | +- string (case-sensitive) |
| 28 | +- number (integer) |
| 29 | +- array (deep equality) |
| 30 | +- object (deep equality) |
| 31 | + |
| 32 | +## Examples |
| 33 | + |
| 34 | +### Example 1 - Find the last rollout slot for a server role (strings) |
| 35 | + |
| 36 | +Use `lastIndexOf()` to locate where a particular role (like a web node) |
| 37 | +appears last in a planned rollout sequence. This is handy when you need to |
| 38 | +schedule a final step (for example, draining traffic) after the last matching |
| 39 | +node has been processed. This example uses [`createArray()`][02] to build the |
| 40 | +list of nodes. |
| 41 | + |
| 42 | +```yaml |
| 43 | +# lastindexof.example.1.dsc.config.yaml |
| 44 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 45 | +resources: |
| 46 | +- name: Rollout Plan |
| 47 | + type: Microsoft.DSC.Debug/Echo |
| 48 | + properties: |
| 49 | + output: |
| 50 | + lastWebIndex: "[lastIndexOf(createArray('web01','db01','web02','cache01','web03'), 'web03')]" |
| 51 | + lastWebFamilyIndex: "[lastIndexOf(createArray('web01','db01','web02','cache01','web02'), 'web02')]" |
| 52 | +``` |
| 53 | +
|
| 54 | +```bash |
| 55 | +dsc config get --file lastindexof.example.1.dsc.config.yaml |
| 56 | +``` |
| 57 | + |
| 58 | +```yaml |
| 59 | +results: |
| 60 | +- name: Rollout Plan |
| 61 | + type: Microsoft.DSC.Debug/Echo |
| 62 | + result: |
| 63 | + actualState: |
| 64 | + output: |
| 65 | + lastWebIndex: 4 |
| 66 | + lastWebFamilyIndex: 4 |
| 67 | +messages: [] |
| 68 | +hadErrors: false |
| 69 | +``` |
| 70 | +
|
| 71 | +Note that string comparison is case-sensitive. Searching for `WEB02` would |
| 72 | +return `-1` in this example. |
| 73 | + |
| 74 | +### Example 2 - Locate the last matching configuration object (objects) |
| 75 | + |
| 76 | +Deep equality lets you search arrays of objects. Here we find the last |
| 77 | +occurrence of a feature flag object with a specific name. This example uses |
| 78 | +[`createObject()`][03] to build objects and [`createArray()`][10] to build the |
| 79 | +collection. |
| 80 | + |
| 81 | +```yaml |
| 82 | +# lastindexof.example.2.dsc.config.yaml |
| 83 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 84 | +resources: |
| 85 | +- name: Feature Flags |
| 86 | + type: Microsoft.DSC.Debug/Echo |
| 87 | + properties: |
| 88 | + output: |
| 89 | + lastBetaIndex: "[lastIndexOf(createArray(createObject('name','Beta'), createObject('name','Gamma'), createObject('name','Beta')), createObject('name','Beta'))]" |
| 90 | +``` |
| 91 | + |
| 92 | +```bash |
| 93 | +dsc config get --file lastindexof.example.2.dsc.config.yaml |
| 94 | +``` |
| 95 | + |
| 96 | +```yaml |
| 97 | +results: |
| 98 | +- name: Feature Flags |
| 99 | + type: Microsoft.DSC.Debug/Echo |
| 100 | + result: |
| 101 | + actualState: |
| 102 | + output: |
| 103 | + lastBetaIndex: 2 |
| 104 | +messages: [] |
| 105 | +hadErrors: false |
| 106 | +``` |
| 107 | + |
| 108 | +Property order in objects doesn't matter. The following also returns `1` due to |
| 109 | +deep equality: `lastIndexOf(array(createObject('a',1,'b',2), createObject('b',2,'a',1)), createObject('a',1,'b',2))`. |
| 110 | + |
| 111 | +## Parameters |
| 112 | + |
| 113 | +### arrayToSearch |
| 114 | + |
| 115 | +The array to search. Required. |
| 116 | + |
| 117 | +```yaml |
| 118 | +Type: array |
| 119 | +Required: true |
| 120 | +Position: 1 |
| 121 | +``` |
| 122 | + |
| 123 | +### itemToFind |
| 124 | + |
| 125 | +The item to search for. Required. |
| 126 | + |
| 127 | +```yaml |
| 128 | +Type: string | number | array | object |
| 129 | +Required: true |
| 130 | +Position: 2 |
| 131 | +``` |
| 132 | + |
| 133 | +## Output |
| 134 | + |
| 135 | +Returns a number representing the last index or -1 if not found. |
| 136 | + |
| 137 | +```yaml |
| 138 | +Type: number |
| 139 | +``` |
| 140 | + |
| 141 | +## Related functions |
| 142 | + |
| 143 | +- [`indexOf()`][00] - First occurrence index in an array |
| 144 | +- [`contains()`][01] - Checks for presence in arrays/objects/strings |
| 145 | + |
| 146 | +<!-- Link reference definitions --> |
| 147 | +[00]: ./indexOf.md |
| 148 | +[01]: ./contains.md |
| 149 | +[02]: ./createArray.md |
| 150 | +[03]: ./createObject.md |
0 commit comments