Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Kevin:



underscore.js.



Challenge accepted!   Next article (today(?)).  



Steven.


Steve:



You should have a look at the object extend operator in underscore for your server-side lab.   I have found issues with other implementations of extend running on this version of rhino don't behave as expected when mixing objects.   I was working with another extend function from github last month, and in browser testing, objects could be mixed ( _.extend ) both directions (A into B, B into A), and all the keys and values however deeply nested would properly merge.   But I found that this version of Rhino doesn't behave the same when pushing deeply nested keys from obj B into A, when the keys didnt exist in A already.   I think I was testign this with keys where the values were objects...and it descended at-least 3 levels deep.



this is the library I was using where I observed the behavior: object-extend/extend.js at master · bernhardw/object-extend · GitHub



Sorry i cant seem to find my test cases from the day i ran into this.



I believe it was something like:



var a = {


      'a' : 'test 1',


      'b' : 'test 2',


      'c' : {


                          'z': 'nested 1',


                          'y' :   { 'q' : 'nested 2'   }


                      },


};



var b= {


      'd' : 'test 3',


      'e' :   'test 4',


      'f' :   {


          'm' : { 'j' : 'nested 3' } ,


          'n' : 'nested 4',


      },


      'g' : 'test '5',


}



gs.log(_.extend(a,b))


gs.log(_.extend(b,a))



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"


Geneva - JavaScript Rhino (server-side) version - ECMA 3.     Use polyfills from MDN   for any newer javascript functionality



Helsinki - JavaScript Rhino (server-side) version - ECMA 5       supports newer javascript functions such as string.trim() and array.indexOf()



ECMAScript 5 compatibility table