Highlighted
Outstanding Contributor.
Outstanding Contributor.
1798 views

HPSM - JavaScript - how to push only unique data into array

Jump to solution

Hello experts,

Do you know, how can I push only unique data from JS query into array?

I have tried indexOf or set from ES6 JavaScript but they don't work in HPSM...

Best regards,

Dino

0 Likes
1 Solution

Accepted Solutions
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: HPSM - JavaScript - how to push only unique data into array

Jump to solution

I can recommend using Underscore lib to accomplish this. SM (at least version 9.41 and greater) includes it's own version of this JS library. (What's Underscore, see -> http://underscorejs.org/# )

About your problem, please check this thread and its accepted solution:

https://stackoverflow.com/questions/17350363/add-only-unique-objects-to-an-array-in-javascript

---
Moving on, this account is no longer active. Best regards, Kelalek
- So Long, and Thanks for All the Fish

View solution in original post

8 Replies
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: HPSM - JavaScript - how to push only unique data into array

Jump to solution

I can recommend using Underscore lib to accomplish this. SM (at least version 9.41 and greater) includes it's own version of this JS library. (What's Underscore, see -> http://underscorejs.org/# )

About your problem, please check this thread and its accepted solution:

https://stackoverflow.com/questions/17350363/add-only-unique-objects-to-an-array-in-javascript

---
Moving on, this account is no longer active. Best regards, Kelalek
- So Long, and Thanks for All the Fish

View solution in original post

Highlighted
Honored Contributor.. Honored Contributor..
Honored Contributor..

Re: HPSM - JavaScript - how to push only unique data into array

Jump to solution

Hi @dfilipovic2,

I have tried indexOf or set from ES6 JavaScript but they don't work in HPSM...

I have asked the same in the Blog entry for "What's new in HPSM 9.50".
The answer is:

R&D have let me know currently Service Manager's RTE is using SpiderMonkey 1.5, which can support ECMA 3

You can find the official answer here: (i have highlighted the answer)
https://community.microfocus.com/t5/IT-Service-Management/Introducing-HPE-Service-Manager-9-50/bc-p/226500/highlight/true#M172

But as @Kelalek2 already wrote - Use the Underscore Library.

Highlighted
Outstanding Contributor.
Outstanding Contributor.

Re: HPSM - JavaScript - how to push only unique data into array

Jump to solution

Hello @Kelalek2,

how to use this library?
Let's say that I want to use _.uniq(array);
I have started it like this lib.Underscore._.uniq(array); and I get that _ is not defined at char 1.

Can you help me?

Best regards,
Dino

0 Likes
Highlighted
Outstanding Contributor.
Outstanding Contributor.

Re: HPSM - JavaScript - how to push only unique data into array

Jump to solution

Ok, I have found it 🙂

lib.Underscore.require().uniq(array)

First I need to initialize require() so I can use underscore library. 🙂

Best regards,

Dino

0 Likes
Highlighted
Honored Contributor.. Honored Contributor..
Honored Contributor..

Re: HPSM - JavaScript - how to push only unique data into array

Jump to solution

Hi,

just a tip:

define a global variable at the beginning of your SL:

var _ = system.library.Underscore.require();

Then you can copy&paste most of the stuff 🙂

With:

_.str.capitalize("foo bar");

You have access to the Underscore String methods: http://epeli.github.io/underscore.string/

Highlighted
Outstanding Contributor.
Outstanding Contributor.

Re: HPSM - JavaScript - how to push only unique data into array

Jump to solution

@MarcusReinhardt, thank you for your input. 🙂
That is even better! 🙂

Best regards,
Dino

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: HPSM - JavaScript - how to push only unique data into array

Jump to solution

Hi,

Using Underscore framework is a good approch.

For understanding more about JS works, I found this implementation usefull:

 

function dedup(arr)
{
    var obj = {};
    for(var a=0;a<arr.length;a++) obj[arr[a]]=true;

    var resultarr = [];
   for(o in obj) resultarr.push(o);

   return resultarr;
}


var test = [ 1,5,2,5,6,3,2,6,8,3,2,9];

var s = "";
for(t in test) s+=" "+test[t];
print("Test: "+s);

var s = "";
var dedup = dedup(test)
for(d in dedup) s+=" "+dedup[d];
print("Test: "+s);

 

 

 

This is basically using Object data type for this: Each array element is written as property of an Object - an there can be only one property with same name.

If you then want to return an array, you will need to loop over all properties of the Object and push them to the Array to be returned.

Armin

0 Likes
Highlighted
Honored Contributor.. Honored Contributor..
Honored Contributor..

Re: HPSM - JavaScript - how to push only unique data into array

Jump to solution

Hi @AFranke,

i think your example will work as expected ( I will ignore the syntax/coding style for now 😉 ).

But as you already told, it's better to use UnderscoreJS.

Here my example with Underscore, based on your snippet:

var resObj = _.object(['1', '2', '3', '4', '1', '2']);
var resArr = _.keys(resObj);

The variable resObj contains:

{
1 : undefined,
2 : undefined,
3 : undefined,
4 : undefined 
}

The variable resArr contains:

[1,2,3,4]

 

Documentation reference:

 

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.