1 Reply Latest reply on Aug 1, 2015 6:52 AM by Bernd Alheit

    Need to search then remove an array item every time code loops until nothing remains

    lindaeliseruble Level 1



      I've created a function that searches the array to see if an item exists and then by using splice() will remove that item from an array each time the code loops.


      The first time the code loops through, I get what I expect.  An item is located in the array and then it is spliced out of the array as illustrated below:


      Array Items before splice(): 0.3,21.4,0.0,0.0,1.2,10.8,0.4,3.1,2.4,14.7,6.9,0.0,28.3,1.3,7.3,1.8

      Array Items after splice(): 21.4,0.0,0.0,1.2,10.8,0.4,3.1,2.4,14.7,6.9,0.0,28.3,1.3,7.3,1.8


      The "0.3" had been spliced out of the code.  I was hoping to locate the next array item and splice it out of the array items that were left the next time the code goes through the loop, however, this does not happen.  Instead, I wind up with the original set of array items and I am back to square one.


      I realize that each time the function one2oneComp is called, the array is replenished with the original items.  How do I prevent this from happening?

      Below is the function I wrote.  I'm sure one of you Experts will know the answer.


      Thanks in advance.




      function one2oneComp(doc, p, matchTerm, mode, wordsInRect, loopCntr) {
               var results = [];
               var splicedMT = [];

               var z = wordsInRect.indexOf(matchTerm);
               if (z != -1) {

                    results.push({matchTerm: matchTerm, page: p, index: loopCntr});  
                    return results;