Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE
Highlighted
dfilipovic2 Outstanding Contributor.
Outstanding Contributor.
678 views

Problem with Underscore.js library function _.difference

Hello,
I am having a problem with _.difference function when I am calling a function from LINK when I have used it for the purpose of comparing two arrays, one of them is current file field and the other one is queried list of items inside of cirelationship.

I have solved the problem with two for loops but I don't want to use that I want to understand the problem why _.difference is not working.
I when I compare A, B it returns only values from A and A is list all of queried data from cirelationship while B is currently placed data in field array.
When I switch them it only returns B values... Can someone explain that to me? :)

function searchModules(aplikacija, cpgModuli){

                var sql="logical.name = \"" + aplikacija + "\"";

                var f1 = new SCFile("cirelationship", SCFILE_READONLY);

                var rc =f1.doSelect(sql);

                if(rc == RC_SUCCESS){

                               var moduli = new Array();

                               moduli = f1.related_cis;

               

                               // Provjera unique paramatera:

                for(var i = 0; i < moduli.length(); i++){

                    var found = false;

                               var unique = new Array();

                               for(var j = 0; j < cpgModuli.length(); j++){ // j < is missed;

                                               if(moduli[i] == cpgModuli[j]){

                                                               //print(moduli[i]);

                                                               found = true;

                                                               break;

                                               }

                   }

                                               if(found == false){

                                               //print(moduli[i]);

                                               // Pusham odmah u varijablu polje!

                                               system.vars.$polje.push(moduli[i]);

                  }

                }

               

                //print("Radim provjeru koju sam dobio od supporta: " + lib.Underscore.require().difference(moduli, cpgModuli));

               

                               // Stari push podataka:

                               /*

                               for(var i = 0; i < moduli.length();i++){

                                               //print("Ispis modula: " + moduli[i]);

                                               system.vars.$polje.push(moduli[i]);

 

                               }

                               */

                }

}

0 Likes
4 Replies
Knowledge Partner Knowledge Partner
Knowledge Partner

Re: Problem with Underscore.js library function _.difference

Without provide data example it's not possible to say anything about it. The behavior looks ok. Check the explanation of each function at Underscore library... For example the difference function bellow

  // Take the difference between one array and a number of other arrays.
  // Only the elements present in just the first array will remain.
  _.difference = function(array) {
    var rest = concat.apply(ArrayProto, slice.call(arguments, 1));
    return _.filter(array, function(value){ return !_.contains(rest, value); });
  };

 

I think what you wanna do is intersection, no?

var a = new Array(1,2,3,4,5);
var b = new Array(3,5,7)

print(lib.Underscore.require().difference(a, b)); // result is:1,2,4

//if you want what is common you need use intersection.
print(lib.Underscore.require().intersection(a, b)); // result is 3,5

 

Regards,
Breno Abreu

If you feel this was helpful please click the KUDOS! thumb below!
dfilipovic2 Outstanding Contributor.
Outstanding Contributor.

Re: Problem with Underscore.js library function _.difference

Hi @BrenoAbreu,
Problem is that it's not doing what it should do, in my example:
field modules contains = ["Module1", "Module2", "Module3"];
By querying into DB i get = ["Module1", "Module2", "Module3", "Module4"];

I put the arrays like this _.difference(queryfromdb, fieldmodules); //Returns ["Module1", "Module2", "Module3", "Module4"];

It should return only "Module4"...

Best regards,
Dino

0 Likes
Acclaimed Contributor.. Kelalek2 Acclaimed Contributor..
Acclaimed Contributor..

Re: Problem with Underscore.js library function _.difference

I did a following test:

var _ = lib.Underscore.require();
var arr1 = ["Module1", "Module2", "Module3","Module4"];
var arr2=["Module1","Module2","Module3"];

print (_.difference(arr1, arr2));

Print is "Module4". Looks it works as it should. If the result you get with your data is different, I assume there's something wrong with the array's values?

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

Re: Problem with Underscore.js library function _.difference

The parameter order is important for this function, re-read calmly the method comment I posted you above. 

Even thought, the return is very very strange.... check the case, hidden spaces, etc.. make sure they are really equal.. 

Regards,
Breno Abreu

If you feel this was helpful please click the KUDOS! thumb below!
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.