Range selector (anIndex to anotherIndex)
If you need multiple sequential values from an Array, DataWeave allows you to select a range of values with the range selector ([n to m]). Instead of returning a single value like the index selector does, it will return an Array of values:
[“prod”, “qa”, “dev”]
%dw 2.0
output json
—
payload[0 to 1]
[“prod”, “qa”]
Just like with the index selector, negative indexes can be used to select a range of values starting from the end of the Array.
The to selector returns values of matching indices in an array or string. You can also use it to reverse the order of the indices in the range. The selector treats characters in the string as indices.
Range selectors limit the output to only the elements specified by the range on that specific order. This selector allows you to slice an array or even invert it.
DataWeave Script:
%dw 2.0
output application/json
—
{
slice: [0,1,2][0 to 1],
last: [0,1,2][-1 to 0]
}
Output JSON:
{
“slice”: [
0,
1
],
“last”: [
2,
1,
0
]
}
The Range selector limits the output to only the elements specified by the range on that specific order, treating the string as an array of characters. This selector enables you to slice a string or invert it.
DataWeave Script:
%dw 2.0
output application/json
—
{
slice: “DataWeave”[0 to 1],
middle : “superfragilisticexpialadocious”[10 to 13],
last: “DataWeave”[-1 to 0]
}
Output JSON:
{
“slice”: “Da”,
“middle”: “list”,
“last”: “evaeWataD”
}
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…