DataWeave Interview Question: Concatenate Elements of an Array
This article will help you practice your DataWeave skills in MuleSoft. We're going to use two different approaches to concatenate an array of letters.
Join the DZone community and get the full member experience.
Join For FreeThis article will help you practice your DataWeave skills in MuleSoft. Let's get started.
Input:
["m","u","l","e","s","o","f","t"]
Output:
xxxxxxxxxx
"mulesoft"
Let's talk about the solution now.
We will apply a reduce function to our input and add the value of the item with the value of the accumulator.
Approach 1 Code:
xxxxxxxxxx
%dw 2.0
output application/json
---
payload reduce ((item, accumulator) -> accumulator ++ item)
Approach 2 Code:
xxxxxxxxxx
%dw 2.0
output application/json
---
payload reduce ($$ ++ $)
Here, $$ stands for accumulator and $ stands for an item.
Output:
"mulesoft"
If you want reverse output then you can use any of the above approaches with a little modification.
Approach 1 Code:
xxxxxxxxxx
%dw 2.0
output application/json
---
payload reduce ((item, accumulator) -> item ++ accumulator)
Approach 2 Code:
xxxxxxxxxx
%dw 2.0
output application/json
---
payload reduce ($ ++ $$)
Output:
xxxxxxxxxx
"tfoselum"
Happy Learning!
Opinions expressed by DZone contributors are their own.
Comments