Forum Discussion
ionutbarna
5 years agoNew Contributor II
Yes but .map() function transforms every element from the Array, I only need to change the fields where there are numbers. Is there a way that I can use conditional logic inside of the .map() function?( if typeof value == ‘number’ then value * 10 else value
Absolutely - the expression in the map function can use ternary logic just like you have in your example updating the object elements.
Assuming an input array:
{ "myArray": [ "abc", 13, "def", 15 ] }
This works as I believe you are requesting:
$myArray.map(x => x instanceof Number ? x * 10 : x)
{ "updatedArray": [ "abc", 130, "def", 150 ] }