Forum Discussion

aditya_gupta41's avatar
aditya_gupta41
Contributor
4 years ago

Formatting multiple array values in a single expression

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

  • bojanvelevski's avatar
    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)