Bicep の object 関数

この記事では、オブジェクトを操作するための Bicep 関数について説明します。

contains

contains(container, itemToFind)

配列に値が含まれるかどうか、オブジェクトにキーが含まれるかどうか、または文字列に部分文字列が含まれるかどうかを確認します。 文字列比較では大文字・小文字を区別します。 ただし、オブジェクトにキーが含まれているかどうかをテストする場合、比較で大文字・小文字を区別しません。

名前空間: sys

パラメーター

パラメーター 必須 タイプ 説明
container はい 配列、オブジェクト、文字列 検索対象の値を含む値。
itemToFind はい 文字列または整数 検索対象の値。

戻り値

項目が見つかった場合は True、それ以外の場合は False です。

次の例では、contains をさまざまな型で使用する方法を示します。

param stringToTest string = 'OneTwoThree'
param objectToTest object = {
  one: 'a'
  two: 'b'
  three: 'c'
}
param arrayToTest array = [
  'one'
  'two'
  'three'
]

output stringTrue bool = contains(stringToTest, 'e')
output stringFalse bool = contains(stringToTest, 'z')
output objectTrue bool = contains(objectToTest, 'one')
output objectFalse bool = contains(objectToTest, 'a')
output arrayTrue bool = contains(arrayToTest, 'three')
output arrayFalse bool = contains(arrayToTest, 'four')

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
stringTrue Bool True
stringFalse Bool False
objectTrue Bool True
objectFalse Bool False
arrayTrue Bool True
arrayFalse Bool False

empty

empty(itemToTest)

配列、オブジェクト、または文字列が空かどうかを判断します。

名前空間: sys

パラメーター

パラメーター 必須 タイプ 説明
itemToTest はい 配列、オブジェクト、文字列 空かどうかを確認する値。

戻り値

値が空の場合は True を、値が空でない場合は False を返します。

次の例では、配列、オブジェクト、および文字列が空かどうかを確認します。

param testArray array = []
param testObject object = {}
param testString string = ''

output arrayEmpty bool = empty(testArray)
output objectEmpty bool = empty(testObject)
output stringEmpty bool = empty(testString)

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
arrayEmpty Bool True
objectEmpty Bool True
stringEmpty Bool True

intersection

intersection(arg1, arg2, arg3, ...)

パラメーターから共通の要素を持つ 1 つの配列またはオブジェクトを返します。

名前空間: sys

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい 配列またはオブジェクト 共通の要素の検索に使用する 1 番目の値。
arg2 はい 配列またはオブジェクト 共通の要素の検索に使用する 2 番目の値。
残りの引数 いいえ 配列またはオブジェクト 共通の要素の検索に使用する残りの値。

戻り値

共通の要素を持つ配列またはオブジェクト。

次の例では、intersection を配列およびオブジェクトと共に使用する方法を示します。

param firstObject object = {
  one: 'a'
  two: 'b'
  three: 'c'
}
param secondObject object = {
  one: 'a'
  two: 'z'
  three: 'c'
}
param firstArray array = [
  'one'
  'two'
  'three'
]
param secondArray array = [
  'two'
  'three'
]

output objectOutput object = intersection(firstObject, secondObject)
output arrayOutput array = intersection(firstArray, secondArray)

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
objectOutput Object {"one": "a", "three": "c"}
arrayOutput Array ["two", "three"]

items

items(object)

ディクショナリ オブジェクトを配列に変換します。 配列をオブジェクトに変換する方法については、「toObject」を参照してください。

名前空間: sys

パラメーター

パラメーター 必須 タイプ 内容
object はい object 配列に変換するディクショナリ オブジェクト。

戻り値

変換されたディクショナリのオブジェクトの配列。 配列内の各オブジェクトには、ディクショナリのキー値を含む key プロパティがあります。 各オブジェクトには、オブジェクトのプロパティを含む value プロパティもあります。

次の例では、ディクショナリ オブジェクトが配列に変換されます。 配列内のオブジェクトごとに、変更された値を使用して新しいオブジェクトが作成されます。

var entities = {
  item002: {
    enabled: false
    displayName: 'Example item 2'
    number: 200
  }
  item001: {
    enabled: true
    displayName: 'Example item 1'
    number: 300
  }
}

var modifiedListOfEntities = [for entity in items(entities): {
  key: entity.key
  fullName: entity.value.displayName
  itemEnabled: entity.value.enabled
}]

output modifiedResult array = modifiedListOfEntities

上記の例では、以下が返されます。

"modifiedResult": {
  "type": "Array",
  "value": [
    {
      "fullName": "Example item 1",
      "itemEnabled": true,
      "key": "item001"
    },
    {
      "fullName": "Example item 2",
      "itemEnabled": false,
      "key": "item002"
    }
  ]
}

次の例は、items 関数から返される配列を示しています。

var entities = {
  item002: {
    enabled: false
    displayName: 'Example item 2'
    number: 200
  }
  item001: {
    enabled: true
    displayName: 'Example item 1'
    number: 300
  }
}

var entitiesArray = items(entities)

output itemsResult array = entitiesArray

この例では、以下が返されます。

"itemsResult": {
  "type": "Array",
  "value": [
    {
      "key": "item001",
      "value": {
        "displayName": "Example item 1",
        "enabled": true,
        "number": 300
      }
    },
    {
      "key": "item002",
      "value": {
        "displayName": "Example item 2",
        "enabled": false,
        "number": 200
      }
    }
  ]
}

JSON では、オブジェクトは 0 個以上のキーと値のペアの順序付けられていないコレクションです。 順序付けは実装によって異なる可能性があります。 たとえば、Bicep items() 関数では、アルファベット順でオブジェクトを並べ替えます。 他の場所では、元の順序を保持できます。 この非決定性のため、デプロイのパラメーターと出力と対話するコードを記述するときは、オブジェクト キーの順序について想定することは避けてください。

json

json(arg1)

有効な JSON 文字列を JSON データ型に変換します。

名前空間: sys

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい string JSON に変換する値。 文字列は、適切に書式設定された JSON 文字列である必要があります。

戻り値

指定された文字列の JSON データ型。null が指定された場合は空のオブジェクト。

解説

JSON オブジェクトにパラメーター値または変数を含める必要がある場合、 concat関数を使用し、関数に渡す文字列を作成します。

次の例では、json 関数を使用する方法を示します。 空のオブジェクトに null を渡すことができることに注意してください。

param jsonEmptyObject string = 'null'
param jsonObject string = '{\'a\': \'b\'}'
param jsonString string = '\'test\''
param jsonBoolean string = 'true'
param jsonInt string = '3'
param jsonArray string = '[[1,2,3]]'
param concatValue string = 'demo value'

output emptyObjectOutput bool = empty(json(jsonEmptyObject))
output objectOutput object = json(jsonObject)
output stringOutput string =json(jsonString)
output booleanOutput bool = json(jsonBoolean)
output intOutput int = json(jsonInt)
output arrayOutput array = json(jsonArray)
output concatObjectOutput object = json(concat('{"a": "', concatValue, '"}'))

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
emptyObjectOutput Boolean True
objectOutput Object {"a": "b"}
stringOutput String テスト
booleanOutput Boolean True
intOutput Integer 3
arrayOutput Array [ 1, 2, 3 ]
concatObjectOutput Object { "a": "demo value" }

length

length(arg1)

配列内の要素、文字列内の文字、またはオブジェクト内のルート レベル プロパティの数を返します。

名前空間: sys

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい array、string、または object 要素の数を取得するために使用する配列、文字の数を取得するために使用する文字列、またはルート レベル プロパティの数を取得するために使用するオブジェクト。

戻り値

整数。

次の例では、length を配列および文字列と共に使用する方法を示します。

param arrayToTest array = [
  'one'
  'two'
  'three'
]
param stringToTest string = 'One Two Three'
param objectToTest object = {
  propA: 'one'
  propB: 'two'
  propC: 'three'
  propD: {
      'propD-1': 'sub'
      'propD-2': 'sub'
  }
}

output arrayLength int = length(arrayToTest)
output stringLength int = length(stringToTest)
output objectLength int = length(objectToTest)

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
arrayLength int 3
stringLength int 13
objectLength int 4

union

union(arg1, arg2, arg3, ...)

パラメーターからすべての要素を持つ 1 つの配列またはオブジェクトを返します。 配列の場合、重複する値は 1 回含められます。 オブジェクトの場合、重複するプロパティ名は 1 回だけ含められまれます。

名前空間: sys

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい 配列またはオブジェクト 要素の結合に使用される 1 番目の値。
arg2 はい 配列またはオブジェクト 要素の結合に使用される 2 番目の値。
残りの引数 いいえ 配列またはオブジェクト 要素の結合に使用される残りの値。

戻り値

配列またはオブジェクト。

注釈

UNION 関数では、パラメーターのシーケンスを使用して、結果の順序と値を決定します。

配列の場合、関数により、最初のパラメーターの各要素が反復処理され、まだ存在していない場合は結果に追加されます。 次に、2 番目のパラメーターと追加のパラメーター (ある場合) に対して処理が繰り返されます。 値が既に存在する場合は、配列内の以前の配置が保持されます。

オブジェクトの場合、最初のパラメーターのプロパティ名と値が結果に追加されます。 以降のパラメーターでは、結果に新しい名前 (ある場合) が追加されます。 後のパラメーターに同じ名前のプロパティがある場合、既存の値はその値で上書きされます。 プロパティの順序は保証されません。

Union 関数は、最上位レベルの要素だけでなく、その中の入れ子になったオブジェクトも再帰的にマージします。 入れ子になった配列値はマージされません。 次のセクションの 2 番目の例を参照してください。

次の例では、union を配列およびオブジェクトと共に使用する方法を示します。

param firstObject object = {
  one: 'a'
  two: 'b'
  three: 'c1'
}

param secondObject object = {
  three: 'c2'
  four: 'd'
  five: 'e'
}

param firstArray array = [
  'one'
  'two'
  'three'
]

param secondArray array = [
  'three'
  'four'
  'two'
]

output objectOutput object = union(firstObject, secondObject)
output arrayOutput array = union(firstArray, secondArray)

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
objectOutput Object {"one": "a", "two": "b", "three": "c2", "four": "d", "five": "e"}
arrayOutput Array ["one", "two", "three", "four"]

次の例は、ディープ マージ機能を示しています:

var firstObject = {
  property: {
    one: 'a'
    two: 'b'
    three: 'c1'
  }
  nestedArray: [
    1
    2
  ]
}
var secondObject = {
  property: {
    three: 'c2'
    four: 'd'
    five: 'e'
  }
  nestedArray: [
    3
    4
  ]
}
var firstArray = [
  [
    'one'
    'two'
  ]
  [
    'three'
  ]
]
var secondArray = [
  [
    'three'
  ]
  [
    'four'
    'two'
  ]
]

output objectOutput object = union(firstObject, secondObject)
output arrayOutput array = union(firstArray, secondArray)

前の例からの出力は次のようになります。

名前 Type
objectOutput Object {"property":{"one":"a","two":"b","three":"c2","four":"d","five":"e"},"nestedArray":[3,4]}
arrayOutput Array [["one","two"],["three"],["four","two"]]

入れ子になった配列がマージされた場合、objectOutput.nestedArray の値は [1, 2, 3, 4] になり、arrayOutput の値は [["one", "two", "three", ["three", "four", "two"]] になります。

次のステップ