I would like to use multiple "indexOf" in a field map script.

Daiki Shinohara
Tera Contributor

I would like to use multiple "indexOf" in a field map script.

I have created multiple conditions using "if" and "else if".
The first "indexOf" works, but the second and subsequent "indexOf" don't work.

Do you know how to deal with this?
Or if you have an example, I would appreciate it if you could describe it.


Thank you in advance.

1 ACCEPTED SOLUTION

Try as below:

answer = (function transformEntry(source) {

    var result = [];
    if (source.fieldnameA.indexOf('x') > -1) {
        result.push("xxxxx");
    if (source.fieldnameB.indexOf('y') > -1) {
        result.push("yyyyy");
    if (source.fieldnameC.indexOf('z') > -1) {

       result.push("zzzzz");
    }

    return result;
})(source);

Best Regards
Aman Kumar

View solution in original post

6 REPLIES 6

ajayr
Giga Expert

Hello Daiki,

Can you just post your script here

 

Thanks,

Ajay

Hello Ajay

It is described this way.

In this case, only "x" returns, but "y" and "z" do not.

 


answer = (function transformEntry(source) {
    if (source.fieldnameA.indexOf('x') > -1) {
        return "xxxxx";
    } else if (source.fieldnameB.indexOf('y') > -1) {
        return "yyyyy";
    } else if (source.fieldnameC.indexOf('z') > -1) {
        return "zzzzz";
    }
})(source);

 


Thank you in advance.

Try as below:

answer = (function transformEntry(source) {

    var result = [];
    if (source.fieldnameA.indexOf('x') > -1) {
        result.push("xxxxx");
    if (source.fieldnameB.indexOf('y') > -1) {
        result.push("yyyyy");
    if (source.fieldnameC.indexOf('z') > -1) {

       result.push("zzzzz");
    }

    return result;
})(source);

Best Regards
Aman Kumar

OlaN
Giga Sage
Giga Sage

Hi,

There are many ways to solve this, if you could describe your use case a bit more you can more adequate help.

One simple way to solve it, is to store the text you want to find in variables, and later use those variables in if-conditions or a switch-case statement.

var test = 'My test string with many words in it';
var stringExist = test.indexOf('string');
var helloExist = test.indexOf('hello');
var inExist = test.indexOf('in');

gs.info('string: ' + stringExist);
gs.info('hello: ' + helloExist);
gs.info('in: ' + inExist);

/* Output:
*** Script: string: 8
*** Script: hello: -1
*** Script: in: 11
*/