The flatten to move the elements from the subarrays to the parent array, eliminate the subarrays, and covert all key-value pairs into a list of objects within the parent array. With the help of flatten function multiple arrays are bounded together as single array.
Syntax:
flatten<T, Q>(@StreamCapable items: Array<Array<T> | Q>): Array<T | Q>
Turns a set of subarrays (such as [ [1,2,3], [4,5,[6]], [], [null] ]
) into a single, flattened array (such as [ 1, 2, 3, 4, 5, [6], null ]
).
Name | Description |
---|---|
items | The input array of arrays made up of any supported types. |
[ [1,2,3], [4,5,[6]], [], [null] ]
%dw 2.0
output application/json
---
flatten(payload)
[
1,
2,
3,
4,
5,
[
6
],
null
]
Note that it flattens only the first level of subarrays and omits empty subarrays.
This example defines three arrays of numbers, creates another array containing those three arrays, and then uses the flatten function to convert the array of arrays into a single array with all values.
%dw 2.0
output application/json
var array1 = [1,2,3]
var array2 = [4,5,6]
var array3 = [7,8,9]
var arrayOfArrays = [array1, array2, array3]
---
flatten(arrayOfArrays)
[ 1,2,3,4,5,6,7,8,9 ]
If you like my post please follow me to read my latest post on programming and technology.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…