cancel
Showing results for 
Search instead for 
Did you mean: 

Formatting multiple array values in a single expression

aditya_gupta41
Contributor

Hello,

I want to create an expression where multiple array values can be changes in a single expression.

For eg.
Input - [1,4,20,22,30].
In this I just want to change the values which are less than 10 and put a ‘0’ before those filtered value.
Output - [01,04,20,22,30]

Can anyone suggest what will be the best possible way to achieve this?

Thanks in Advance

1 REPLY 1

bojanvelevski
Valued Contributor

Try this in a mapper:

$array.map(x=> parseInt(x) < 10 ? '0'+ x : x.toString())

I included parsing of the elements to ensure handling string numbers. Note that the result of the expression will be an array of strings, which is inevitable due to the numbers with a leading zero (<10)