DataWeave Interview Question: Find the Greatest and Smallest Number of an Array
This article will help you practice your DataWeave skills in MuleSoft. Use the article as a tutorial for learning more about DataWeave.
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:
[1,2,3,4,5]
Output:
5
Let's talk about the solution now.
In the first step, we will store the first value into a variable.
In the second step, we will use a reduce function to iterate over an array and find the greatest number.
Code for Greatest Number:
%dw 2.0
output application/json
var startNumber=payload[0]
---
payload reduce ((item, accumulator=startNumber) -> if(item>accumulator) item else accumulator)
The above code will provide us with the Greatest Number of an Array. We will use the below code to find the Smallest Number.
Code for Smallest Number:
%dw 2.0
output application/json
var startNumber=payload[0]
---
payload reduce ((item, accumulator=startNumber) -> if(item<accumulator) item else accumulator)
Output:
1
Happy Learning!
Opinions expressed by DZone contributors are their own.
Comments