Highlighted
Established Member..
Established Member..
4991 views

HP Service Manager javascript array function indexOf

Jump to solution

Why when I do this:-

 

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");
print(a);

 

do I get this:-

 

 

this is valid javascript which should return 2

 

 

0 Likes
1 Solution

Accepted Solutions
Highlighted
Established Member..
Established Member..

Re: HP Service Manager javascript array function indexOf

Jump to solution

Found an example in the SRC script library creating a function on the fly:-

 

var fruits = ["banana","Orange","Apple","Mango"];

fruits.indexOf = function (o) {
	for (var i=0; i<this.length; ++i)
		if (this[i] == o)
			return i;
	return -1;
}


print(fruits.indexOf('Mango'));

 

Also, there is an array index of function in the ArrayUtils library so you can use that one too.

 

View solution in original post

4 Replies
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: HP Service Manager javascript array function indexOf

Jump to solution

Hello,

It works in some JavaScript versions, for an array. In Service Manager you have to giv it a string

 

thi will work

var afruits=["Banana","Orange","Apple"];
var afruitsstr=afruits.toString();
var a=afruitsstr.indexOf("Apple");
print(a);

 

Highlighted
Established Member..
Established Member..

Re: HP Service Manager javascript array function indexOf

Jump to solution

Found an example in the SRC script library creating a function on the fly:-

 

var fruits = ["banana","Orange","Apple","Mango"];

fruits.indexOf = function (o) {
	for (var i=0; i<this.length; ++i)
		if (this[i] == o)
			return i;
	return -1;
}


print(fruits.indexOf('Mango'));

 

Also, there is an array index of function in the ArrayUtils library so you can use that one too.

 

View solution in original post

Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: HP Service Manager javascript array function indexOf

Jump to solution

Maybe I'm late, but I'd use the following code:

 

var fruits = new Array("Orange", "Apple", "Banana", "Mango");
var a = fruits.indexOf("Apple");
print (a);

 

HPSM Javascript needs declaration of an Array var.

Output of this code is 1.

Highlighted
New Member..
New Member..

Re: HP Service Manager javascript array function indexOf

Jump to solution

lib.ArrayUtil.indexOf( array, "string" )

Tags (1)
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.