bag_zip()

Learn how to use bag_zip() to merge two dynamic arrays into a single property-bag of keys and values.

Creates a dynamic property-bag from two input dynamic arrays. In the resulting property-bag, the values from the first input array are used as the property keys, while the values from the second input array are used as corresponding property values.

Syntax

bag_zip(KeysArray, ValuesArray)

Parameters

NameTypeRequiredDescription
KeysArraydynamic✔️An array of strings. These strings represent the property names for the resulting property-bag.
ValuesArraydynamic✔️An array whose values will be the property values for the resulting property-bag.

Returns

Returns a dynamic property-bag.

Examples

The following example shows how to use bag_zip() to create a property-bag from two arrays. The first array contains the keys, and the second array contains the values. The resulting property-bag contains the keys and values zipped together.

let Data = datatable(KeysArray: dynamic, ValuesArray: dynamic) [
    dynamic(['a', 'b', 'c']), dynamic([1, '2', 3.4])
];
Data
| extend NewBag = bag_zip(KeysArray, ValuesArray)
KeysArrayValuesArrayNewBag
[‘a’,‘b’,‘c’][1,‘2’,3.4]{‘a’: 1,‘b’: ‘2’,‘c’: 3.4}

The following example shows how to use bag_zip() when the arrays have different lengths. In this case, the resulting property-bag contains null values for the missing keys.

let Data = datatable(KeysArray: dynamic, ValuesArray: dynamic) [
    dynamic(['a', 'b', 'c']), dynamic([1, '2'])
];
Data
| extend NewBag = bag_zip(KeysArray, ValuesArray)
KeysArrayValuesArrayNewBag
[‘a’,‘b’,‘c’][1,‘2’]{‘a’: 1,‘b’: ‘2’,‘c’: null}

The following example shows how to use bag_zip() when the arrays have different lengths. In this case, the resulting property-bag contains null values for the missing keys.

let Data = datatable(KeysArray: dynamic, ValuesArray: dynamic) [
    dynamic(['a', 'b']), dynamic([1, '2', 2.5])
];
Data
| extend NewBag = bag_zip(KeysArray, ValuesArray)
KeysArrayValuesArrayNewBag
[‘a’,‘b’][1,‘2’,2.5]{‘a’: 1,‘b’: ‘2’}

The following example demonstrates how bag_zip() handles cases where the keys array contains non-string values. Any key that isn’t a string is excluded from the resulting property-bag.

let Data = datatable(KeysArray: dynamic, ValuesArray: dynamic) [
    dynamic(['a', 8, 'b']), dynamic([1, '2', 2.5])
];
Data
| extend NewBag = bag_zip(KeysArray, ValuesArray)
KeysArrayValuesArrayNewBag
[‘a’,8,‘b’][1,‘2’,2.5]{‘a’: 1,‘b’: 2.5}

The following example demonstrates how bag_zip() behaves when the parameter intended to be an array of values is not actually an array. In this case, all resulting property values are set to null.

let Data = datatable(KeysArray: dynamic, ValuesArray: dynamic) [
    dynamic(['a', 8, 'b']), dynamic(1)
];
Data
| extend NewBag = bag_zip(KeysArray, ValuesArray)
KeysArrayValuesArrayNewBag
[‘a’,8,‘b’]1{‘a’: null,‘b’: null}

The following example demonstrates how bag_zip() behaves when the parameter intended to be an array of keys is not actually an array. In this case, the resulting property-bag is null.

let Data = datatable(KeysArray: dynamic, ValuesArray: dynamic) [
    dynamic('a'), dynamic([1, '2', 2.5])
];
Data
| extend NewBag = bag_zip(KeysArray, ValuesArray)
| extend IsNewBagEmpty=isnull(NewBag)

| KeysArray | ValuesArray | NewBag | IsNewBagEmpty | |–|–|–| | a | [1,‘2’,2.5] | | TRUE |