JavaScript:比较两个数组并返回匹配的索引

时间:2017-02-06 14:48:21

标签: javascript arrays

我有以下代码。

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;
}

这需要以下数组。

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

唯一的目的是找到匹配并在'all_form_numbers数组上返回匹配的索引。

在调用代码时

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

我得到以下输出。

Match Found: 6

这是正确的,但是当我将all_form_Numbers数组改为

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

我得到以下输出。

Match Found: 1,1

有人可以帮助我,所以它应该输出;

Match Found 1, 6.

感谢。

4 个答案:

答案 0 :(得分:3)

试试这个:

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;
}

答案 1 :(得分:1)

当你这样做时

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

您正在数组array2_large中搜索值0871355066的索引两次,并且根据indexOf的定义,它将返回指定值的第一次出现的位置。

这就是为什么自第一次出现的索引以来索引值1两次。

对于解决方案只需将变量i值推入数组ary即可。这已经是循环中array2_large的索引值。

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你只需要推送索引i这里是固定代码(你也可以将数组声明为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;
}

答案 3 :(得分:0)

通过首先创建位置查找表,可以在O(n + m)运行时复杂度上解决这个问题。然后将每个元素从第一个数组映射到所有位置,并在Set中收集这些索引,只留下唯一值。

试试这个:

&#13;
&#13;
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))
&#13;
&#13;
&#13;