Question

I am trying to find a string in a javascript array in the transformer of a mirth channel. Mirth throws an error when I try to use indexOf function. My understanding is that indexOf is something that browsers add in, rather than a native part of the javascript language itself. ( How do I check if an array includes an object in JavaScript? )

So is array.indexOf just not supported in Mirth? Is there any way to use .indexOf in Mirth? Maybe an alternate syntax? Or do I need to just loop thru the array to search?

Was it helpful?

Solution

Mirth uses the Rhino engine for Javascript, and on some earlier versions of the JVM, indexOf appeared to not be supported on arrays. Since upgrading our JVM to 1.6.23 (or higher), indexOf has started working. However, we still have legacy code that, when searching arrays of strings, I just use a loop each time:

var compareString = "blah";
var index = -1;
for (var i = 0; i < myArray.length; ++i)
{
    if (myArray[i] == compareString)
    {
        index = i;
        break;
    }
}

If you need to do this frequently, you should be able to use a code template to manually add the indexOf function to Array.

Set the code template to global access, and try out something like this (untested code):

Array.prototype.indexOf = function(var compareObject)
{
    for (var i = 0; i < myArray.length; ++i)
    {
        // I don't think this is actually the right way to compare
        if (myArray[i] == compareObject)
        {
            return i;
        }
    }

    return -1;
}

OTHER TIPS

This is how I search arrays in a Mirth js transformer:

var Yak = [];
Yak.push('test');

if(Yak.indexOf('test') != -1)
{
    // do something
}

Does this give you error?

var i = ['a', 'b', 'c']

if(i.indexOf('a') > -1)
{
       ///do this, if it finds something in the array that matches what inside the indexOf()
}
else
{
    //do something else if it theres no match in array
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top