Array .indexOf() method not working

Mike McCall
Giga Guru

I realize this could be a general JavaScript question, but it seems like it should be working in the "real world" (and is failing within Service-Now). Basically, I'm using a business rule to look through an array of values from a custom status field I've added to [grc_remediation]:


     var grRemediations = new GlideRecord('grc_remediation');
...
     var aRemediationStatuses = new Array();
     while (grRemediations.next()) {
           aRemediationStatuses.push(grRemediations.u_status.toLowerCase());
     }
...
     gs.log('aRemediationStatuses: ' + aRemediationStatuses);
     // The statement above will return 'aRemediationStatuses: work in progress,open'.

     gs.log('aRemediationStatuses.indexOf("open"): ' + aRemediationStatuses.indexOf('open'));
     /*
           The statement above will return 'aRemediationStatuses.indexOf("open"): undefined'.
           Shouldn't it return 'aRemediationStatuses.indexOf("open"): 1'?
       */

I need to know where a value is showing up within an array (or if it's not in the array at all), but I just keep getting 'undefined' returned by .indexOf(), and I have verified that the array is properly populated. Any suggestions?
1 ACCEPTED SOLUTION

For anyone that runs across this post as I did today.   A colleague of mine just mentioned .indexOf() works in Helsinki for arrays.   I tried it between Geneva and Helsinki and indeed it does work!



var s='Detroit,London,Tokyo';


var s2 = s.split(',');


var a = s2.indexOf("London");


gs.print(a);



In Geneva I get "undefined"


In Helsinki I get "1"


View solution in original post

17 REPLIES 17

CapaJC
ServiceNow Employee
ServiceNow Employee

Almost. That'll tell you which character position the letter "o" in "open" falls in the entire String. So for the Array:



{ "new", "active", "open", "closed" }

Which gives a toString() of:


new,active,open,closed

You'll get the answer 11 instead of 2, which is the position in the array that was wanted.

You'd also run into trouble if one of the array's values was "opentaco", since that'd return a positive match for the indexOf() check.


Try this... I had a similar problem with using arrays and index of... this which my colleague wrote may help:



function contains(a, obj) {


      var i = a.length;


      while (i--) {


            if (a[i] === obj) {


                    return true;


            }


      }


      return false;


}



//then do



var array = [];


array.push("Badgers");


gs.log(contains(array,"Badgers"))


sabell2012
Mega Sage
Mega Sage

Michael:



Saw this and thought I would throw in my 2-cents on this as well.   This is an ECMAScript 5 function.   ServiceNow JavaScript is 3 or less (2.2.2).   You can add this functionality with ArrayUtil, or extend the JavaScript language base with a prototype function.   The MDN polyfill gives you most everything you need.   I wrote about the prototype extension recently here.



Most of the methods missing (sic. ECMAScript 3 thru 6); if they have a Polyfill definition, can be added into the ServiceNow JavaScript base functionality.



Steven.


Gotta love how the recent update to the developer portal broke so many links.


re-posting a working link for this interesting article by Steve Bell:


Mini-Lab: Extending the GlideRecord Object



Personally, I would like to see a ServiceNow Rhino compatible port of underscore.js.  


Wow!   Thanks Kevin.   Had not realized that the portal switchover had busted the links.