简体   繁体   中英

JavaScript: Compare two arrays and return index of matches

I have the following code.

function findMatch(array_1_small, array2_large) {
var ary = new Array();
for(i = 0;i < array2_large.length; i++)
{
  for(z = 0; z < 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;
}

That takes the following arrays.

var all_SMS_TO = ["0861932936", "0871355066", "0874132026", "0872908445", "0874132026"];
var all_FORM_NUMBERS = ["", "", "", "", "", "", "0871355066",""];

The sole purpose is to find a match and return the index of the match on the 'all_form_numbers array.

On calling the code

var a = findMatch(all_SMS_TO, all_FORM_NUMBERS);
console.log("Match Found " + a);

I get the following output.

Match Found: 6

Which is correct, however when I alter the all_form_Numbers array to

var all_FORM_NUMBERS = ["", "0871355066", "", "", "", "", "0871355066",""];

I get The following output.

Match Found: 1,1

Could somebody help me with this so it should output;

Match Found 1, 6.

Thanks.

try this:

function findMatch(array_1_small, array2_large) {
var ary = new Array();
for(i = 0;i < array2_large.length; i++)
{
  for(z = 0; z < array_1_small.length; z++)
  {
    if(array2_large[i] == array_1_small[z])
    {
      ary.push(i);
    }
  }

}
return ary;
}

When you do

var idx = array2_large.indexOf(array2_large[i]);

you are searching for the index of the value 0871355066 in the array array2_large twice and as per the definition of indexOf it will returns the position of the first occurrence of a specified value.

This is why you are getting index value 1 twice since its the index of first occurrence .

For solution Just push the variable i value into the array ary. Which is already the index value of array2_large in the loop.

 function findMatch(array_1_small, array2_large) { var ary = new Array(); for(i = 0;i < array2_large.length; i++) { for(z = 0; z < array_1_small.length; z++) { if(array2_large[i] == array_1_small[z]) { ary.push(i); } } } return ary; } var all_SMS_TO = ["0861932936", "0871355066", "0874132026", "0872908445", "0874132026"]; //var all_FORM_NUMBERS = ["", "", "", "", "", "", "0871355066",""]; var all_FORM_NUMBERS = ["", "0871355066", "", "", "", "", "0871355066",""]; var a = findMatch(all_SMS_TO, all_FORM_NUMBERS); console.log("Match Found " + a); 

You just need to push the index i here is the fixed code (you can also declare an array as var res = [];

function findMatch(arraySmall, arrayLarge) {
  var res = []
  for (var i = 0; i < arrayLarge.length; i++) {
    for (var j = 0; j < arraySmall.length; j++) {
      if (arrayLarge[i] === arraySmall[j]) {
        res.push(i);
      }
    }
  }
  return res;
}

It is possible to solve this on O(n+m) runtime complexity by creating a lookup table of positions first. Then you map each element from the first array to all positions and collect these indices in a Set to only leave unique values.

Try this:

 var all_SMS_TO = ["0861932936", "0871355066", "0874132026", "0872908445", "0874132026"]; var all_FORM_NUMBERS = ["", "0871355066", "", "", "", "", "0871355066",""]; function findMatch(array_1_small, array2_large) { var positions = Array.from(array2_large.entries()).reduce((acc, t) => { var index = t[0] var element = t[1] if (!acc.hasOwnProperty(element)) { acc[element] = [] } acc[element].push(index) return acc }, {}) var result = new Set() array_1_small.forEach(x => { if (positions[x] === undefined) { return } positions[x].forEach(index => result.add(index)) }) return Array.from(result) } console.log("Match found: " + findMatch(all_SMS_TO, all_FORM_NUMBERS)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM