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]);

 

                               }

                               */

                }

}

  • 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

     

  • Hi ,
    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

  • 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?

  • 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.. 

  • 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..