'Using BICEP can I find a specific element in an array by a substring of the name?

I have a bicep module storageAccounts.bicep, which is creating multiple storage accounts and outputing an array of their names:

@description('Name of Environment')
param environment string

param storageAccounts array = [
  {
    name : 'api${environment}${uniqueString(resourceGroup().id)}'
    skuName : 'Standard_LRS'
    accessTier : 'Hot'
  }
  {
    name : 'web${environment}${uniqueString(resourceGroup().id)}'
    skuName : 'Standard_LRS'
    accessTier : 'Hot'
  }
]

resource storage_resource 'Microsoft.Storage/storageAccounts@2021-06-01' = [for storage in storageAccounts: {
  name : storage.name
  location : resourceGroup().location
  sku:{
    name : storage.skuName
  }
  kind : 'StorageV2'
  properties:{
    accessTier: storage.accessTier
    allowBlobPublicAccess: true
    minimumTlsVersion: 'TLS1_2'
    supportsHttpsTrafficOnly: true
    encryption:{
      keySource: 'Microsoft.Storage'
      services:{
        blob:{
          keyType: 'Account'
          enabled: true
        }
        file:{
          keyType: 'Account'
          enabled: true
        }
      }
    }    
  }
}]

output storageAccounts array = [for (name, i) in storageAccounts:{
    storageAccountName : name
}]

I'm passing that array into another module to create a function app, and I'm trying to find a particular item in the array, something like this:

@description('array of storage account names')
param storageAccounts array

var storageAccountName = storagesAccounts.where(function(storageAccount) {
  return storageAccount.name.startsWith('api')
})


resource storageAccount 'Microsoft.Storage/storageAccounts@2019-04-01' existing = {
  name: storageAccountName
}

Is this something I can do? Or would I be better creating the storage accounts separately and passing the name directly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source