摘要
从输入返回值的数组。
语法
createArray(<inputValue>)
说明
函数 createArray()
从输入值返回值数组。 可以使用此函数创建任何类型的数组。 输入值的类型必须相同 - 数字、字符串、对象或数组。 当输入值为对象或数组时,它们不需要是具有相同属性或相同类型的数组的对象。 当输入值为数组时,函数将返回数组数组。
示例
示例 1 - Create整数数组
示例概要
# createArray.example.1.dsc.config.yaml
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: Echo array of integers
type: Test/Echo
properties:
output: "[createArray(1, 3, 5)]"
dsc config get --document createArray.example.1.dsc.config.yaml config get
results:
- name: Echo array of integers
type: Test/Echo
result:
actualState:
output:
- 1
- 3
- 5
messages: []
hadErrors: false
示例 2 - Create数组
此配置返回一个数组,其中数组中的项也是数组。 第一个子数组仅包含整数。 第二个子数组仅包含字符串。
# createArray.example.2.dsc.config.yaml
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: Create array of arrays
type: Test/Echo
properties:
output: "[createArray(createArray(1,3,5), createArray('a', 'b', 'c'))]"
dsc config get --document createArray.example.2.dsc.config.yaml
results:
- name: Create array of arrays
type: Test/Echo
result:
actualState:
output:
- - 1
- 3
- 5
- - a
- b
- c
messages: []
hadErrors: false
示例 3 - Create字符串的平展数组
此配置使用 concat () 函数连接两个新创建的字符串数组。 它使用 YAML 的折叠多行字符串语法使函数更具可读性。
# createArray.example.3.dsc.config.yaml
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: Echo flattened array
type: Test/Echo
properties:
output: >-
[concat(
createArray('a', 'b', 'c'),
createArray('d', 'e', 'f')
)]
dsc config get --document createArray.example.3.dsc.config.yaml
results:
- name: Echo flattened array
type: Test/Echo
result:
actualState:
output:
- a
- b
- c
- d
- e
- f
messages: []
hadErrors: false
参数
inputValue
函数 createArray()
需要零个或多个相同类型的输入值。 用逗号分隔每个值。 如果任何输入值的类型与第一个值不同,DSC 将返回函数的错误。
Type: [integer, string, number, object, array]
Required: false
MinimumCount: 0
MaximumCount: 18446744073709551615
输出
函数 createArray()
返回值的数组。 当输入值为数组时,返回的值是数组数组,而不是输入值的平展数组。 可以使用 concat () 函数返回字符串数组的平展数组,如 示例 3 所示。
Type: array