Skip to content

Commit 321827f

Browse files
authored
Merge pull request #194906 from tfitzmac/0412indexof
add indexof and lastindexof
2 parents 1b163a1 + 05c392d commit 321827f

File tree

4 files changed

+369
-23
lines changed

4 files changed

+369
-23
lines changed

articles/azure-resource-manager/bicep/bicep-functions-array.md

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Describes the functions to use in a Bicep file for working with arr
44
author: mumian
55
ms.topic: conceptual
66
ms.author: jgao
7-
ms.date: 04/06/2022
7+
ms.date: 04/12/2022
88

99
---
1010
# Array functions for Bicep
@@ -235,6 +235,82 @@ The output from the preceding example with the default values is:
235235
| arrayOutput | String | one |
236236
| stringOutput | String | O |
237237

238+
## indexOf
239+
240+
`indexOf(arrayToSearch, itemToFind)`
241+
242+
Returns an integer for the index of the first occurrence of an item in an array. The comparison is **case-sensitive** for strings.
243+
244+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
245+
246+
### Parameters
247+
248+
| Parameter | Required | Type | Description |
249+
| --- | --- | --- | --- |
250+
| arrayToSearch | Yes | array | The array to use for finding the index of the searched item. |
251+
| itemToFind | Yes | int, string, array, or object | The item to find in the array. |
252+
253+
### Return value
254+
255+
An integer representing the first index of the item in the array. The index is zero-based. If the item isn't found, -1 is returned.
256+
257+
### Examples
258+
259+
The following example shows how to use the indexOf and lastIndexOf functions:
260+
261+
```bicep
262+
var names = [
263+
'one'
264+
'two'
265+
'three'
266+
]
267+
268+
var numbers = [
269+
4
270+
5
271+
6
272+
]
273+
274+
var collection = [
275+
names
276+
numbers
277+
]
278+
279+
var duplicates = [
280+
1
281+
2
282+
3
283+
1
284+
]
285+
286+
output index1 int = lastIndexOf(names, 'two')
287+
output index2 int = indexOf(names, 'one')
288+
output notFoundIndex1 int = lastIndexOf(names, 'Three')
289+
290+
output index3 int = lastIndexOf(numbers, 4)
291+
output index4 int = indexOf(numbers, 6)
292+
output notFoundIndex2 int = lastIndexOf(numbers, '5')
293+
294+
output index5 int = indexOf(collection, numbers)
295+
296+
output index6 int = indexOf(duplicates, 1)
297+
output index7 int = lastIndexOf(duplicates, 1)
298+
```
299+
300+
The output from the preceding example is:
301+
302+
| Name | Type | Value |
303+
| ---- | ---- | ----- |
304+
| index1 |int | 1 |
305+
| index2 | int | 0 |
306+
| index3 | int | 0 |
307+
| index4 | int | 2 |
308+
| index5 | int | 1 |
309+
| index6 | int | 0 |
310+
| index7 | int | 3 |
311+
| notFoundIndex1 | int | -1 |
312+
| notFoundIndex2 | int | -1 |
313+
238314
## intersection
239315

240316
`intersection(arg1, arg2, arg3, ...)`
@@ -368,6 +444,82 @@ The output from the preceding example with the default values is:
368444
| arrayOutput | String | three |
369445
| stringOutput | String | e |
370446

447+
## lastIndexOf
448+
449+
`lastIndexOf(arrayToSearch, itemToFind)`
450+
451+
Returns an integer for the index of the last occurrence of an item in an array. The comparison is **case-sensitive** for strings.
452+
453+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
454+
455+
### Parameters
456+
457+
| Parameter | Required | Type | Description |
458+
| --- | --- | --- | --- |
459+
| arrayToSearch | Yes | array | The array to use for finding the index of the searched item. |
460+
| itemToFind | Yes | int, string, array, or object | The item to find in the array. |
461+
462+
### Return value
463+
464+
An integer representing the last index of the item in the array. The index is zero-based. If the item isn't found, -1 is returned.
465+
466+
### Examples
467+
468+
The following example shows how to use the indexOf and lastIndexOf functions:
469+
470+
```bicep
471+
var names = [
472+
'one'
473+
'two'
474+
'three'
475+
]
476+
477+
var numbers = [
478+
4
479+
5
480+
6
481+
]
482+
483+
var collection = [
484+
names
485+
numbers
486+
]
487+
488+
var duplicates = [
489+
1
490+
2
491+
3
492+
1
493+
]
494+
495+
output index1 int = lastIndexOf(names, 'two')
496+
output index2 int = indexOf(names, 'one')
497+
output notFoundIndex1 int = lastIndexOf(names, 'Three')
498+
499+
output index3 int = lastIndexOf(numbers, 4)
500+
output index4 int = indexOf(numbers, 6)
501+
output notFoundIndex2 int = lastIndexOf(numbers, '5')
502+
503+
output index5 int = indexOf(collection, numbers)
504+
505+
output index6 int = indexOf(duplicates, 1)
506+
output index7 int = lastIndexOf(duplicates, 1)
507+
```
508+
509+
The output from the preceding example is:
510+
511+
| Name | Type | Value |
512+
| ---- | ---- | ----- |
513+
| index1 |int | 1 |
514+
| index2 | int | 0 |
515+
| index3 | int | 0 |
516+
| index4 | int | 2 |
517+
| index5 | int | 1 |
518+
| index6 | int | 0 |
519+
| index7 | int | 3 |
520+
| notFoundIndex1 | int | -1 |
521+
| notFoundIndex2 | int | -1 |
522+
371523
## length
372524

373525
`length(arg1)`
@@ -652,7 +804,7 @@ An array or object.
652804

653805
The union function uses the sequence of the parameters to determine the order and values of the result.
654806

655-
For arrays, the function iterates through each element in the first parameter and adds it to the result if it isn't already present. Then, it repeats the process for the second parameter and any additional parameters. If a value is already present, it's earlier placement in the array is preserved.
807+
For arrays, the function iterates through each element in the first parameter and adds it to the result if it isn't already present. Then, it repeats the process for the second parameter and any more parameters. If a value is already present, its earlier placement in the array is preserved.
656808

657809
For objects, property names and values from the first parameter are added to the result. For later parameters, any new names are added to the result. If a later parameter has a property with the same name, that value overwrites the existing value. The order of the properties isn't guaranteed.
658810

articles/azure-resource-manager/bicep/bicep-functions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Bicep functions
33
description: Describes the functions to use in a Bicep file to retrieve values, work with strings and numerics, and retrieve deployment information.
44
ms.topic: conceptual
5-
ms.date: 04/06/2022
5+
ms.date: 04/12/2022
66
---
77

88
# Bicep functions
@@ -38,9 +38,11 @@ The following functions are available for working with arrays. All of these func
3838
* [concat](./bicep-functions-array.md#concat)
3939
* [contains](./bicep-functions-array.md#contains)
4040
* [empty](./bicep-functions-array.md#empty)
41+
* [indexOf](./bicep-functions-array.md#indexof)
4142
* [first](./bicep-functions-array.md#first)
4243
* [intersection](./bicep-functions-array.md#intersection)
4344
* [last](./bicep-functions-array.md#last)
45+
* [lastIndexOf](./bicep-functions-array.md#lastindexof)
4446
* [length](./bicep-functions-array.md#length)
4547
* [min](./bicep-functions-array.md#min)
4648
* [max](./bicep-functions-array.md#max)

0 commit comments

Comments
 (0)