MAP
The map function is used to transform the data contained in an array. It does this by iterating over the elements in the array and applying a transformation to each element. The result of the transformation is collected together and output as an array of transformed elements.
Iterates over items in an array and outputs the results into a new array.
Here’s the type definition for map
:
map<T, R>(@StreamCapable items: Array<T>, mapper: (item: T, index: Number) -> R): Array<R>
map(Array<T>, ((T, Number) -> R)): Array<R>
There are two type variables in this definition, T
, and R
. T
represents the type of items that the input Array contains. R
represents the type of items that output Array contains. Since map
’s job is to transform every item in an Array, it makes sense that the type of items in the input Array and type of items in the output Array are different. Knowing this, the lambda definition makes sense:
((T, Number) -> R)
The lambda’s job is to take in each item of type T
from the input Array, as well as the index of that item, and return a new item that will be used in the output Array.
{
"employee":[
{
"FirstName":"John",
"LastName":"Smith",
"Email":"john@gmail.com"
},
{
"FirstName":"Bob",
"LastName":"Stryf",
"Email":"bob@gmail.com"
}
],
"Company":{
"name":"IBM"
}
}
%dw 2.0
output application/json
---
{
"candidatedetails": payload.employee map((item,index) -> {
"fname":item.FirstName ++ " " ++ item.LastName
})
}
{
"candidatedetails": [
{
"fname": "John Smith"
},
{
"fname": "Bob Stryf"
}
]
}
[1,2,3,4,5]
%dw 2.0
output json
---
payload map (n, idx) -> n + 1
[2,3,4,5,6]
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…