Transform Map / Transform Script

MStritt
Tera Guru

I am creating a transform script on a source field, and I'm wondering if I can add multiple values/entries in the script. Instead of creating multiple lines for each color, add them in one line. If so, what the syntax would be.

 

For example: 

if (source.u_color == 'Black' 'Blue') {
return 'White';

 

Code:

answer = (function transformEntry(source) {

if (source.u_color == 'Black') {
return 'White';
}
if (source.u_color == 'Blue') {
return 'White';
}

})(source);

 

5 REPLIES 5

kmohammed
Kilo Guru

Yes, you can check for multiple values in one line in a transform script by using the logical OR operator "||"

The syntax for checking multiple values in one if statement would be:
if (source.u_color == 'Black' || source.u_color == 'Blue') {
return 'White';
}

So you can rewrite your code like this:

answer = (function transformEntry(source) {
if (source.u_color == 'Black' || source.u_color == 'Blue') {
return 'White';
}
})(source);

You can also use Array.includes() or Array.indexOf() method to check multiple values in the if statement.
if(['Black','Blue'].includes(source.u_color)) {
return 'White';
}


if(['Black','Blue'].indexOf(source.u_color) > -1) {
return 'White';
}

It's also possible to check multiple values in one line using "in" keyword also.



if(source.u_color in ['Black','Blue']) {
return 'White';
}
if(source.u_color in ['Black','Blue']) {
return 'White';
}

You can also create an array of colors and then use the Array.includes() method to check multiple values.

var colors = ['Black','Blue'];
if(colors.includes(source.u_color)) {
return 'White';
}