4 Replies Latest reply on Dec 18, 2009 7:17 AM by penyfai

    Comparison of arrays to find elements that differ - JS??

    penyfai Level 1

      I think I've got my logic all wrong.

       

      I'm trying to compare two arrays, with the result being that I get a new array with the elements in the first that are missing from the second.

       

      array1 - a,b,c,d,e

      array2 - a,b,c

       

      new array should contain d,e

       

      THIS IS WHAT I'M TRYING.........

       

       

      //--------------------------------------- Function for getting graphics ---------------------------------------

      function getGraphicsFunc()

      {

      //--------------------------test for graphics in Library--------------------------

      var mylib = app.libraries.item(0);

      //get list of graphics in Library

      var libGraphicList = mylib.assets.everyItem().name;

       

      alert(libGraphicList);        <--THIS WORKS FINE

       

      //-------------get list of all graphics available for CID on Pulse_OPI -------------

      var graphicFolder = Folder(basegraphpath + lastdigits + "/");

      var theGraphics = graphicFolder.getFiles();

      var allGraphics = [];

      //match files to Content ID of advert

      for (i=0; i<theGraphics.length; i++)

      {

           //Assuming is 8 numbers sequence is a static number.

           //You can also use grep to match 8 digits >

           // if(theFiles[i].name.match(/^\d{8}/);

      if(theGraphics[i].name.indexOf(adCID)==0)

      {

                allGraphics.push(theGraphics[i].name);

           }

      }

       

      alert(allGraphics);        <--THIS WORKS FINE - the next comparison is the problem.....

       

      //Compare graphic arrays to see which are NOT in library

       

      var missingGraphics = [];

      for (j=0; j<allGraphics.length; j++)

      {

      if(toString(libGraphicList).indexOf(allGraphics[j])==-1)

      {

      missingGraphics.push(allGraphics[j]);

      }

      }

      alert(missingGraphics);        <--THIS ALWAYS PRODUCES THE SAME 'MISSING GRAPHICS'  EVEN IF THEY ARE IN THE INDL

       

      var graphicsFound = (missingGraphics.length)

      alert("There were " + graphicsFound + " graphics found and imported to the library")

      }

       

       

       

      Many thanks in advance and Happy Christmas everyone.....