title: JavaScript: Compare two arrays and return index of matches
body: <p>I have the following code.</p>
  
  <pre><code>function findMatch(array_1_small, array2_large) {
  var ary = new Array();
  for(i = 0;i &lt; array2_large.length; i++)
  {
    for(z = 0; z &lt; array_1_small.length; z++)
    {
      if(array2_large[i] == array_1_small[z])
      {
       var idx = array2_large.indexOf(array2_large[i]);
        ary.push(idx);
      }
    }
  
  }
  return ary;
  }
  </code></pre>
  
  <p>That takes the following arrays.</p>
  
  <pre><code>var all_SMS_TO = ["0861932936", "0871355066", "0874132026", "0872908445", "0874132026"];
  var all_FORM_NUMBERS = ["", "", "", "", "", "", "0871355066",""];
  </code></pre>
  
  <p>The sole purpose is to find a match and return the index of the match on the 'all_form_numbers array.</p>
  
  <p>On calling the code</p>
  
  <pre><code>var a = findMatch(all_SMS_TO, all_FORM_NUMBERS);
  console.log("Match Found " + a);
  </code></pre>
  
  <p>I get the following output.</p>
  
  <pre><code>Match Found: 6
  </code></pre>
  
  <p>Which is correct, however when I alter the all_form_Numbers array to</p>
  
  <pre><code>var all_FORM_NUMBERS = ["", "0871355066", "", "", "", "", "0871355066",""];
  </code></pre>
  
  <p>I get The following output.</p>
  
  <pre><code>Match Found: 1,1
  </code></pre>
  
  <p>Could somebody help me with this so it should output;</p>
  
  <pre><code>Match Found 1, 6.
  </code></pre>
  
  <p>Thanks.</p>